本文介绍了DOM未完全加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用jQuery文档时,即 $(document).ready(function(){} 有没有机会DOM尚未完全加载? p>

我正在使用一些3 派对工具(Telerik的网格),并设置了一个客户端模板来显示一个复选框,就像。代码:

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






我问的原因是我试图将事件挂钩到所有复选框进行监控更改

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

我在Telerik网格代码之外手动选中了一个复选框,并且勾选了复选框,但是telerik中没有一个复选框网格做...



在这种情况下 - 是否有一个工作?

解决方案

HTML页面中静态定义的DOM没有被加载到 $(document).ready()的机会。但是,如果您正在使用动态加载或创建HTML的第三方库,则不能保证图书馆在 $(document).ready()。其实很有可能没有。



你有几个选项:


  1. 您可以找到在第三方库创建HTML之后触发的事件,因此现在存在复选框,然后使用传统的jQuery来连接他们。

  2. 您可以使用jQuery中的 .live()功能捕获甚至在您指定要连接到它们的时候还不存在的DOM对象的事件。您可以在这里阅读关于 .live()的内容:。它的工作原理主要是像一个正常的事件处理程序,除了它仅适用于某些事件,并且在如何阻止传播方面存在一些差异。


When using jQuery document ready, i.e. $(document).ready(function() { } is there any chance that the DOM has not fully loaded yet?

I am using some 3 party tools (Telerik's grid) and have set a client template to display a checkbox instead, just like this. 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:

$(':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...

And in that case - is there a work around?

解决方案

There's no chance that the statically defined DOM in the HTML page is not loaded yet at $(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(). 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.
  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. You can read about .live() here: 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.

这篇关于DOM未完全加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 11:35