检查给定的DOM元素是否准备就绪 [英] Check if a given DOM element is ready

查看:115
本文介绍了检查给定的DOM元素是否准备就绪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用jQuery或JavaScript检查给定选择器/元素的HTML DOM元素是否已准备就绪?

Is there a way of checking if the HTML DOM element/s for a given selector/element are ready yet using jQuery or JavaScript?

查看jQuery api 函数看起来它只能与文档对象一起使用。如果准备好不能用于此目的还有另一种方法吗?

Looking at the jQuery api for the ready function it looks like it can only be used with the document object. If ready cannot be used for this purpose is there another way of doing this?

例如

 $('h1').ready(function()
{
 //do something when all h1 elements are ready
});

显然我可以使用

$(document).ready(function()
{
 //do something when all h1 elements are ready
});

但如果首先加载所有h1,那么特定于h1元素的代码将仅在整个文档之后执行即使它实际上可以提前执行也已准备就绪。

But if all the h1's load first then the code specific to h1 elements will only execute after the whole document is ready even though it could actually execute earlier.

推荐答案

编辑2012
<从jQuery 1.7.0开始,不推荐使用code> live 方法。现在建议使用 .on()事件来附加事件处理程序。这将替换.bind(),. delegate()和.live()。

Edit 2012 The live method is deprecated as of jQuery 1.7.0. The .on() event is now recommended for attaching event handlers. This replaces .bind(), .delegate(), and .live().

请参阅文档: http://api.jquery.com/on/

原始答案

我认为 jQuery .live()事件可能就是你要找的东西。

i think jQuery .live() event might be what you're looking for.

这篇关于检查给定的DOM元素是否准备就绪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆