Javascript jQuery 事件 .load(), .ready(), .unload()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2683072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery events .load(), .ready(), .unload()
提问by Eric
Just a simple question, for the jQuery event. Are the .load(), .ready() and .unload() run in order when the DOM is loaded? The answer seems yes when I see the jQuery Documentation.
只是一个简单的问题,对于 jQuery 事件。加载 DOM 时,.load()、.ready() 和 .unload() 是否按顺序运行?当我看到 jQuery 文档时,答案似乎是肯定的。
<script type="text/javascript">
$(window).load(function () {
// run code
initializeCode();
});
$(document).ready(function() {
//run code that MUST be after initialize
});
$(window).unload(function() {
Cleanup();
});
</script>
However, the code inside the .ready() is execute before the initializeCode(); is execute, so I feel really strange. And now I have to place my code inside the .onload() method and just after the initializeCode(); line, which means to be inside the .ready() block.
但是,.ready() 中的代码在 initializeCode() 之前执行;是execute,所以感觉真的很奇怪。现在我必须将我的代码放在 .onload() 方法中,并在 initializeCode() 之后;行,这意味着在 .ready() 块内。
Could someone explain me more about this, as I am new to jQuery?
由于我是 jQuery 的新手,有人可以就此向我解释更多吗?
回答by Tower
NOTE: .load()& .unload()have been deprecated
注意:.load()&.unload()已被弃用
$(window).load();
Will execute after the page along with all its contents are done loading. This means that all images, CSS (and content defined by CSS like custom fonts and images), scripts, etc. are all loaded. This happens event fires when your browser's "Stop" -icon becomes gray, so to speak. This is very useful to detect when the document along with all its contents are loaded.
将在页面及其所有内容加载完成后执行。这意味着所有图像、CSS(以及由 CSS 定义的内容,如自定义字体和图像)、脚本等都已加载。可以这么说,当您浏览器的“停止”图标变为灰色时,会触发此事件。这对于检测文档及其所有内容何时加载非常有用。
$(document).ready();
This on the other hand will fire as soon as the web browser is capable of running your JavaScript, which happens after the parser is done with the DOM. This is useful if you want to execute JavaScript as soon as possible.
另一方面,一旦 Web 浏览器能够运行您的 JavaScript,这就会触发,这发生在解析器完成 DOM 之后。如果您想尽快执行 JavaScript,这很有用。
$(window).unload();
This event will be fired when you are navigating off the page. That could be Refresh/F5, pressing the previous page button, navigating to another website or closing the entire tab/window.
当您离开页面时将触发此事件。这可能是刷新/F5、按上一页按钮、导航到另一个网站或关闭整个选项卡/窗口。
To sum up, ready() will be fired before load(), and unload() will be the last to be fired.
综上所述,ready() 将在 load() 之前被触发,而 unload() 将是最后被触发的。
回答by David Murdoch
window load will wait for all resources to be loaded.
窗口加载将等待加载所有资源。
document readywaits for the document to be initialized.
document ready等待文档被初始化。
unload well, waits till the document is being unloaded.
卸载好,等到文档被卸载。
the order is: document ready, window load, ... ... ... ... window unload.
顺序是:文件准备好,窗口加载,…………窗口卸载。
always use document readyunless you need to wait for your images to load.
除非您需要等待图像加载,否则始终使用文档就绪。
shorthand for document ready:
文件准备好的简写:
$(function(){
// yay!
});
回答by ljgww
If both "document.ready" variants are used they will both fire, in the order of appearance
如果同时使用“document.ready”变体,它们将按出现的顺序触发
$(function(){
alert('shorthand document.ready');
});
//try changing places
$(document).ready(function(){
alert('document.ready');
});
回答by Praveen Singh
Also, I noticed one more difference between .load and .ready. I am opening a child window and I am performing some work when child window opens. .load is called only first time when I open the window and if I don't close the window then .load will not be called again. however, .ready is called every time irrespective of close the child window or not.
另外,我注意到 .load 和 .ready 之间的另一个区别。我正在打开一个子窗口,当子窗口打开时我正在执行一些工作。.load 仅在我第一次打开窗口时被调用,如果我不关闭窗口,则 .load 将不会再次被调用。但是,无论是否关闭子窗口,每次都会调用 .ready 。

