【问题标题】:jQuery, MVC, jSon - constructing table of divs, filled by call-backsjQuery,MVC,json - 构建 div 表,由回调填充
【发布时间】:2010-06-21 20:23:21
【问题描述】:

我正在尝试使用动态构建信息构建一个表。

我的想法是将 DivA 放在每个单元格中,然后将 DivB 和 DivC 放在 DivA 标记中(DivB 显示“加载”img,DivC 显示内容)。然后,我希望使用对我的 ASP.Net MVC Web 应用程序的 json 调用来更新单元格。到目前为止听起来很疯狂吗?!

<div class="divA">
    <h2>
        Stuff</h2>
    <div class="divB">
        <img src="Images/ajax-loader.gif" />
        <span>Loading</span>
    </div>
    <div class="divC">
    </div>
</div>

使用此代码,我认为我可以激活我的方法并处理回调?

$(document).ready( 
    function() 
    {
        $("div.divA").each(
            function(i)
            {
               updateCellDivs($(this));
            }
         );
    }
 )

但是...我在尝试在下面的“加载”上调用 show 时遇到异常 - 调用 fadeIn 相同的东西。 “加载”对象的类型是 disphtmldivelement - 但 DivA 也是如此。

我不知所措 - 我做错了什么?!

感谢任何cmets,

安德斯,丹麦。

updateCellDivs = function(DivA)
    { 
        jQuery.getJSON("/Home/GetSomething",
        function(json)
        {
            mainDiv  = DivA[0];

            divs = $("div", mainDiv);

            loading  = divs[0];
            contents = divs[1];

            // Abort if there is no data to update.
            if (contents.length==0)
            {
                return;
            }

            loading.show(1000);
            //contents.fadeOut(1000);

            // If no response was given, the data was not yet available.
            // Ask again in two seconds!
            if (json == "")
            {
                setTimeout(updateCellDivs, 2*1000);
                return;
            }                

            DivA.fadeOut(1000);
            //loading.fadeOut(1000);


            DivA.fadeIn(1000);

        });         
    }

【问题讨论】:

    标签: jquery json html


    【解决方案1】:

    问题出在这两行

    loading  = divs[0];
    contents = divs[1];
    

    jQuery 的索引器返回实际的 html 元素。所以你不能直接对它们执行任何jQuery函数,比如show/hide。您必须将它们包装在 jQuery 对象中,如下所示:

    loading  = $(divs[0]);
    contents = $(divs[1]);
    

    see the live working demo here.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      • 2012-07-13
      • 1970-01-01
      • 2018-11-12
      • 2011-12-01
      • 2013-04-16
      • 1970-01-01
      相关资源
      最近更新 更多