【问题标题】:$(document).ready() not executed for ajax loaded content$(document).ready() 未针对 ajax 加载的内容执行
【发布时间】:2010-02-04 23:05:52
【问题描述】:

在返回的部分页面中不执行 document.ready 的原因可能是什么? 它工作正常 2 次,但在第 3 次更新 html 后没有任何反应:

alert(html);
alert($(PopUpItem));
$(PopUpItem).html(html);
alert('in set popup html completed'); 

我已执行所有警报,PopupItem 和 html 具有正确的值。 我正在使用 JQuery 通过 $.post 执行服务器调用。

另外,如果发生一些语法错误,我可以有错误处理程序来捕获吗?

【问题讨论】:

    标签: jquery


    【解决方案1】:

    在您返回的 AJAX 中,您不需要 $(document).ready() 调用。 DOM 已经加载。像这样简单的东西应该可以解决问题:

    <script type="text/javascript">
    // Do something here.
    </script>
    

    【讨论】:

      【解决方案2】:

      您是说传递给$(PopUpItem).html()html 字符串包含一个&lt;script&gt; 块,其中包含document.ready(function() { ... });

      如果是这样,ready 事件处理程序没有触发的原因是脚本根本没有运行。请参阅具有相同“第三次”行为的 this question

      不要将&lt;script&gt; 标签加载到标记中。它根本不是可靠的跨浏览器,jQuery 或没有 jQuery。

      【讨论】:

        【解决方案3】:

        document.ready 仅在页面最初加载时执行。

        为了有一个错误处理程序,您可以使用更多的 $.ajax 来执行您的 ajax 调用 (http://api.jquery.com/jQuery.ajax/)。

        【讨论】:

          【解决方案4】:

          要将事件绑定到任何异步生成的元素,您应该查看 livequery:http://docs.jquery.com/Plugins/livequery

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2010-11-21
            • 1970-01-01
            • 2012-09-03
            • 2011-05-26
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多