在JQuery中,我如何检查DOM是否准备好了? [重复]

时间:2022-08-17 07:20:01

Possible Duplicate:
javascript domready?

可能重复:javascript domready?

I want to check whether $(function(){ }); is "ready".

我想检查是否$(function(){});准备好了”。

Return true if DOM is ready, false otherwise

如果DOM准备好则返回true,否则返回false

4 个解决方案

#1


21  

From jQuery code

来自jQuery代码

if ( jQuery.isReady ) {  
    fn.call( document, jQuery );
} 

#2


14  

You can use the explicit call

您可以使用显式调用

$(document).ready(function(){
  // do this after dom is ready
});

Or use the shortcut

或者使用快捷方式

$(function(){
  // do this after dom is ready
});

It's also useful to wrap your jQuery in an anonymous function when you're using other libraries; Also very common to use this when writing jQuery plugins.

当您使用其他库时,将jQuery包装在匿名函数中也很有用;在编写jQuery插件时也很常见。

(function($, window){
  // use $ here freely if you think any other library might have overridden it outside.
  $(function(){
    // do this after dom is ready
  });
})(jQuery, window);

Lastly, you can use jQuery.isReady (bool)

最后,你可以使用jQuery.isReady(bool)

if (jQuery.isReady) {
  // do something
}

#3


2  

That function will only execute when the dom is ready. That's the point of wrapping your code in that function ;). Having said that, some things (like images, deferred scripts, etc.) might not be fully loaded or rendered yet, so be aware.

该功能仅在dom准备就绪时执行。这就是将代码包装在该函数中的重点;)。话虽如此,有些东西(如图像,延迟脚本等)可能还没有完全加载或渲染,所以要注意。

#4


1  

You can use jQuery.isReady, but it's undocumented and should be avoided.

您可以使用jQuery.isReady,但它没有文档,应该避免使用。

If you just need to run something after the DOM is ready, you can just call

如果你只需要在DOM准备就绪后运行一些东西,你就可以打电话

$(document).ready(function() {
    ...
});

as normal – that will run the code immediately if the DOM is ready and wait until it is if not.

正常情况下 - 如果DOM准备好,将立即运行代码,如果没有,则等待直到。

#1


21  

From jQuery code

来自jQuery代码

if ( jQuery.isReady ) {  
    fn.call( document, jQuery );
} 

#2


14  

You can use the explicit call

您可以使用显式调用

$(document).ready(function(){
  // do this after dom is ready
});

Or use the shortcut

或者使用快捷方式

$(function(){
  // do this after dom is ready
});

It's also useful to wrap your jQuery in an anonymous function when you're using other libraries; Also very common to use this when writing jQuery plugins.

当您使用其他库时,将jQuery包装在匿名函数中也很有用;在编写jQuery插件时也很常见。

(function($, window){
  // use $ here freely if you think any other library might have overridden it outside.
  $(function(){
    // do this after dom is ready
  });
})(jQuery, window);

Lastly, you can use jQuery.isReady (bool)

最后,你可以使用jQuery.isReady(bool)

if (jQuery.isReady) {
  // do something
}

#3


2  

That function will only execute when the dom is ready. That's the point of wrapping your code in that function ;). Having said that, some things (like images, deferred scripts, etc.) might not be fully loaded or rendered yet, so be aware.

该功能仅在dom准备就绪时执行。这就是将代码包装在该函数中的重点;)。话虽如此,有些东西(如图像,延迟脚本等)可能还没有完全加载或渲染,所以要注意。

#4


1  

You can use jQuery.isReady, but it's undocumented and should be avoided.

您可以使用jQuery.isReady,但它没有文档,应该避免使用。

If you just need to run something after the DOM is ready, you can just call

如果你只需要在DOM准备就绪后运行一些东西,你就可以打电话

$(document).ready(function() {
    ...
});

as normal – that will run the code immediately if the DOM is ready and wait until it is if not.

正常情况下 - 如果DOM准备好,将立即运行代码,如果没有,则等待直到。