从函数内部调用$(document).ready()是否安全?

T N*_*yen 5 javascript jquery javascript-events

如果我在$(document).ready()函数中使用处理程序,它是否仍然保证其中的代码只有在文档准备就绪时才会运行,即使文档就绪事件在过去发生得很好?

Rah*_*ane 14

是.

来自jQuery ready函数.

// Catch cases where $(document).ready() is called after the
// browser event has already occurred.
if ( document.readyState === "complete" ) {
    // Handle it asynchronously to allow scripts the opportunity to delay ready
    return setTimeout( jQuery.ready, 1 );
}
Run Code Online (Sandbox Code Playgroud)