DOM 未完全加载?
当使用 jQuery 文档就绪时,即 $(document).ready(function() { }
是否有可能 DOM 尚未完全加载?
我正在使用一些 3rd 派对工具(Telerik 的网格)并设置了一个客户端模板来显示复选框,就像 this:
.ClientTemplate("<input type='checkbox' name='checkedRecords' value='<#= OrderID #>' />")
我问的原因是我试图将一个事件连接到所有复选框以监视更改
:
$(':input').change(
function () {
alert('you fired!');
});
我手动将一个复选框放在了外部Telerik 网格代码,它连接到复选框更改,但 Telerik 网格内的复选框都没有......
在这种情况下 - 有解决方法吗?
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 3rd 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 live,
Edit
.live() 从版本 1.7 开始已弃用,并从版本 1.9 起被删除:
相反,实时使用 on()
Try using live,
Edit
.live() is deprecated form version 1.7 and is removed since version 1.9:
Instead live use on()
HTML 页面中静态定义的 DOM 不可能在
$(document).ready()
处尚未加载。但是,如果您使用动态加载或创建 HTML 的第三方库,则无法保证该库在$(document).ready()
时已完成其任务。事实上,很可能还没有。您有几个选择:
.live()
功能来捕获在您指定要连接到它们时尚不存在的 DOM 对象的事件。您可以在此处阅读有关.live()
的信息:http://api.jquery.com /直播/。它的工作原理与普通事件处理程序基本相似,只是它仅适用于某些事件,并且在停止传播的方式上存在一些差异。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:
.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.