简体   繁体   English

DOM没有满载?

[英]DOM not fully loaded?

When using jQuery document ready, ie $(document).ready(function() { } is there any chance that the DOM has not fully loaded yet? 当使用jQuery文档时,即$(document).ready(function() { }是否有可能DOM尚未完全加载?

I am using some 3 rd party tools (Telerik's grid) and have set a client template to display a checkbox instead, just like this . 我使用一些第三方工具(Telerik的网格),并建立一个客户端模板来显示一个复选框来代替,就像这样 Code: 码:

.ClientTemplate("<input type='checkbox' name='checkedRecords' value='<#= OrderID #>' />")

The reason I ask is that I am attempting to hook up an event to all checkboxes to monitor change : 我问的原因是我试图将事件连接到所有复选框以监视change

$(':input').change(
    function () {
       alert('you fired!');
});

I put a checkbox manually outside of the Telerik grid code, and it hooks up to the checkbox changing, but none of the checkboxes inside the telerik grid do... 我在Telerik网格代码之外手动设置了一个复选框,它连接到复选框更改,但telerik网格内的复选框都没有...

And in that case - is there a work around? 在那种情况下 - 是否有解决方法?

Try using live, 尝试使用直播,

$(':input').live('change', function() {
                alert('you fired!');
            });

Edit 编辑

.live() is deprecated form version 1.7 and is removed since version 1.9: Instead live use on() .live()已从1.7版本中弃用,自版本1.9起被删除:而是在()上实时使用

There's no chance that the statically defined DOM in the HTML page is not loaded yet at $(document).ready() . HTML页面中静态定义的DOM不可能在$(document).ready()加载。 But, if you're using a third party library that is dynamically loading or creating HTML, there is no guarantee that the library has done it's business at the time of $(document).ready() . 但是,如果您正在使用动态加载或创建HTML的第三方库,则无法保证库在$(document).ready()时已经完成了它的业务。 In fact, it's very likely that is has not. 事实上,它很可能没有。

You have a couple options: 你有几个选择:

  1. You can find an event that is triggered after the third party library has created it's HTML and thus the checkboxes now exist and then use tranditional jQuery to hook up to them. 您可以找到在第三方库创建HTML之后触发的事件,因此现在存在复选框,然后使用传统的jQuery来连接它们。
  2. You can use the .live() capabilities in jQuery to capture events for even DOM objects that don't exist yet at the time you specify that you want to hook up to them. 您可以使用jQuery中的.live()功能捕获甚至在您指定要连接它们.live()不存在的DOM对象的事件。 You can read about .live() here: http://api.jquery.com/live/ . 你可以在这里阅读.live()http//api.jquery.com/live/ It works mostly like a normal event handler except that it only works for some events and there are some differences in how you might stop propogation. 它主要像普通事件处理程序一样工作,只是它只适用于某些事件,并且在如何停止传播方面存在一些差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM