简体   繁体   English

窗口和文档之间的加载/就绪事件的差异

[英]difference of load/ready events between window and document

Is there a difference between $(window).ready(function(){}) and $(document).ready(function(){})? $(window).ready(function(){})$(document).ready(function(){})?之间有区别$(document).ready(function(){})?

If so, what is it? 如果是这样,它是什么?

In that same vein, what is the difference between $(window).load(function(){}); 同样, $(window).load(function(){});之间的区别是什么$(window).load(function(){}); and $(document).load(function(){}); $(document).load(function(){}); ?

In researching this and other "ready" issues, I think I've found the difference care of this question. 在研究这个和其他“准备好”的问题时,我想我已经找到了这个问题的不同之处

Here is the ready function handler in jQuery. 这是jQuery中的ready函数处理程序。

ready: function( fn ) {
    // Attach the listeners
    jQuery.bindReady();
    // Add the callback
    readyList.add( fn );
        return this;
},

It seems as though you can add any element (even complete jibberish) into this function and its callback will be added to the readyList. 看起来好像你可以在这个函数中添加任何元素(甚至是完整的乱码),它的回调将被添加到readyList中。 It also seems that when the document is ready, it will trigger all of the callbacks in readyList, regardless of whether they are part of the document or not. 当文档准备就绪时,它似乎会触发readyList中的所有回调,无论它们是否是文档的一部分。

See this fiddle for an example: http://jsfiddle.net/w5k5t/2/ 看一下这个小提琴的例子: http//jsfiddle.net/w5k5t/2/

I have not fully tested an order to these ready-calls, but a brief inspection of the code leads me to believe they will be executed synchronously in the order the callbacks are added. 我还没有完全测试这些现成调用的命令,但是对代码的简要检查使我相信它们将按照添加回调的顺序同步执行。

Therefore, $(document).ready and $(window).ready are synonymous, just as is $('aoeuaoeuaoeu').ready is synonymous, and each will likely fire in the order they are declared. 因此,$(document).ready和$(window).ready是同义词,就像$('aoeuaoeuaoeu').ready是同义词,并且每个都可能按照声明的顺序触发。

document ready event executes already when the HTML-Document is loaded and the DOM is ready, even if all the graphics haven't loaded yet. 文档就绪事件在加载HTML文档并且DOM准备就绪时已经执行,即使所有图形尚未加载。
The window load event executes a bit later when the complete page is fully loaded, including all frames, objects and images. 窗口加载事件稍后在完整加载完整页面时执行,包括所有帧,对象和图像。 Therefore functions which concern images or other page contents should be placed in the load event for the window or the content tag itself. 因此,涉及图像或其他页面内容的功能应该放在窗口或内容标签本身的加载事件中。

refer link1 link2 参考link1 link2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 嵌套 $(document).ready() 和 $(window).load() 事件之间的区别 - Difference between nested $(document).ready() and $(window).load() events $(window).load和$(document).ready之间有什么区别? - What is the difference between $(window).load and $(document).ready? jQuery-$(document).ready(和$(document).on('ready page:load' - jquery - what is the difference between $(document).ready( and $(document).on('ready page:load' $(document).ready中的多个$(document).ready和$(window).load - multiple $(document).ready and $(window).load in $(document).ready $(document).bind('ready',function)和$(document).ready(function(){})之间有什么区别 - What are the difference between $(document).bind('ready', function) and $(document).ready(function() {}) $(document).ready(initialize)和$(document).on('ready',initialize)之间有什么区别 - what is the difference between $(document).ready(initialize) and $(document).on('ready',initialize) 为什么$(window).load()工作但不是$(document).ready()? - Why does $(window).load() work but not $(document).ready()? 替代window.load和document.ready - Alternative to window.load and document.ready document.ready和window.load不起作用 - document.ready and window.load not working 编写$(window).scroll()和有什么区别? 在document.ready()内部和外部? - What is the difference between writing $(window).scroll(); inside document.ready() and outside it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM