【问题标题】:Load content into expandable DIV with ajax and jquery使用 ajax 和 jquery 将内容加载到可扩展的 DIV 中
【发布时间】:2012-06-16 02:25:12
【问题描述】:

我想使用可折叠 DIV 向用户显示和隐藏内容。

我找到了这个 jQuery 代码来进行展开和折叠: http://webcloud.se/code/jQuery-Collapse/

但是内容已经加载到 div 中(只是隐藏在视图之外)。

然后我发现了这个: http://www.raymondcamden.com/index.cfm/2011/4/5/Collapsible-content-and-Ajax-loading-with-jQuery-Mobile

它将内容加载到打开的 div 中,但在关闭时也将其卸载!

然而,它都与 jQuery mobile 混合在一起,所以它的样式。 我希望能够自己设置 div 的样式。第一个示例也使用漂亮的反弹或淡入淡出效果来显示内容。

这样做的原因是我想向用户显示不同的内容,例如图像或 Flash 文件,但我不希望在页面加载时将所有内容都加载到页面中,这将是太多的东西。

那么如何使用第一个 jQuery Collapse 示例,但要加载外部页面?

【问题讨论】:

  • 这个问题不是很具体,也不是很清楚,但我会尽我所能来理解你所追求的。听起来您想使用ajax 进行服务器调用并将折叠的div 替换为ajax 响应字符串。
  • 我想使用这个:webcloud.se/code/jQuery-Collapse,但有来自不同页面的内容加载到该区域。像这样:coldfusionjedi.com/demos/april52011/test.cfm,但具有第一个链接(jQuery-Collapse)为您提供的样式和额外选项(例如动画)。这有帮助吗?
  • 您可能会觉得这很有帮助:stackoverflow.com/a/27139899/1922144

标签: ajax jquery jquery-plugins jquery-mobile


【解决方案1】:

我喜欢这个问题,所以我花了一点时间制作一些接近插件的东西:

//only run the event handler for collapsible widgets with the "data-url" attribute
$(document).delegate('.ui-collapsible[data-url] > .ui-collapsible-heading', 'click', function () {

    //cache the collapsible content area for later use
    var $this = $(this).siblings('.ui-collapsible-content');

    //check if this widget has been initialized yet
    if (typeof $this.data('state') === 'undefined') {

        //initialize this widget

        //update icon to gear to show loading (best icon in the set...)
        $this.siblings('.ui-collapsible-heading').find('.ui-icon').removeClass('ui-icon-plus').addClass('ui-icon-gear')

        //create AJAX request for data, in this case I'm using JSONP for cross-domain abilities
        $.ajax({

            //use the URL specified as a data-attribute on the widget
            url           : $this.closest('.ui-collapsible').data('url'),
            type          : 'get',
            dataType      : 'jsonp',
            success       : function (response) {

                //get the height of the new content so we can animate it into view later
                var $testEle   = $('<div style="position:absolute;left:-9999px;">' + response.copy + '</div>');
                $('body').append($testEle);
                var calcHeight = $testEle.height();

                //remove the test element
                $testEle.remove();

                //get data to store for this widget, also set state
                $this.data({
                    state         : 'expanded',
                    height        : calcHeight,
                    paddingTop    : 10,
                    paddingBottom : 10

                //add the new content to the widget and update it's css to get ready for being animated into view
                }).html('<p>' + response.copy + '</p>').css({
                    height        : 0,
                    opacity       : 0,
                    paddingTop    : 0,
                    paddingBottom : 0,
                    overflow      : 'hidden',
                    display       : 'block'

                //now animate the new content into view
                }).animate({
                    height        : calcHeight,
                    opacity       : 1,
                    paddingTop    : $this.data('paddingTop'),
                    paddingBottom : $this.data('paddingBottom')
                }, 500);

                //re-update icon to minus
                $this.siblings('.ui-collapsible-heading').find('.ui-icon').addClass('ui-icon-minus').removeClass('ui-icon-gear')
            },

            //don't forget to handle errors, in this case I'm just outputting the textual message that jQuery outputs for AJAX errors
            error         : function (a, b, c) { console.log(b); }
        });
    } else {

        //the widget has already been initialized, so now decide whether to open or close it
        if ($this.data('state') === 'expanded') {

            //update state and animate out of view
            $this.data('state', 'collapsed').animate({
                height        : 0,
                opacity       : 0,
                paddingTop    : 0,
                paddingBottom : 0
            }, 500);
        } else {

            //update state and animate into view
            $this.data('state', 'expanded').animate({
                height        : $this.data('height'),
                opacity       : 1,
                paddingTop    : $this.data('paddingTop'),
                paddingBottom : $this.data('paddingBottom')
            }, 500);
        }
    }

    //always return false to handle opening/closing the widget by ourselves
    return false;
});​

可折叠的 HTML 如下所示:

<div data-role="collapsible" data-url="http://www.my-domain.com/jsonp.php">
    <h3>Click Me</h3>
    <p></p>
</div>

这是一个演示:http://jsfiddle.net/YQ43B/6/

请注意,对于演示,我发现使初始动画平滑的最佳方法是添加以下 CSS:

.ui-mobile .ui-page .ui-collapsible .ui-collapsible-content {
    padding-top    : 0;
    padding-bottom : 0;
}​

jQuery Mobile 添加的默认填充是10px 用于顶部和底部填充,我将这些值作为数据属性添加到每个小部件以保持默认值。

请注意,此代码可以稍微调整以显示其他类型的内容,我使用 JSONP 只是因为您可以在 JSFiddle 上使用它。

【讨论】:

  • 我现在正在尝试,但无论我做什么,文字总是被切断。即使我直接从 jsfiddle 复制代码:S 但这似乎确实是正确的类型,我认为我最大的问题将是样式它,因此它没有任何移动 css 样式。
【解决方案2】:

这是我做的一个例子:

         $(document).ready(function () {
               $('.accordian_body').hide();
                 });


         $('.accordian_head').click(function () {
            $(this).next().animate(
        { 'height': 'toggle' }, 'fast'
        );

然后在 HTML 中

   <div class="accordian_head"></div>
   <div class="accordian_body"></div>

在 CSS 中,你可以随意设置头部和身体的样式,并在后面添加代码 -

   <asp:Literal ID="lit1" runat="server" />


    foreach(DataRow dr in DataTable.Rows)
     {
         lit1.Text = lit1.Text + "<div class=\"accordian_head\">" Whatever you want... "</div><div class=\"accordian_body\">" Whatever in body "</div>
      }

【讨论】:

  • 我认为您在第一段代码的末尾错过了一个 })。我尝试在 jsfiddle 中实现它,但我无法让它工作。我是这类事情的初学者,所以我可能没有正确实施。
  • 这里没有 Ajax,内容只是被预加载到 div 中。
  • 如果你想让它在 jsfiddle 中工作——你必须在 _head 和 _body div 中放一些东西,我只是在做一个例子,因为你说你想动态填充它们——所以我提供了 asp:从后面的代码中填充它们的字面量
  • 好吧,我可以使用第一个链接示例预加载内容,这不是我想要做的,因为它加载了太多东西。我想让用户说出何时加载内容。因为也许他们不想。 @Scott,哦,我明白了。我想知道asp的东西。是的,它将是一个 php 页面,所以它确实需要 jquery 和 ajax。感谢您的尝试
【解决方案3】:

检查此link 它在 php 中,并显示如何加载到 DIV 的外部链接 Ajax 只能加载内部页面并且不能跨域工作。

【讨论】:

  • 我要链接的页面在我的域中,一个是图片,另一个是 Flash 文件。我希望它在用户点击而不是在页面制作时加载。我的页面已经是 php 并为此加载值。
猜你喜欢
  • 2018-10-14
  • 2014-08-17
  • 1970-01-01
  • 2011-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-15
  • 1970-01-01
相关资源
最近更新 更多