前端window.onload 和 $(document).ready() 的区别

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了前端window.onload 和 $(document).ready() 的区别相关的知识,希望对你有一定的参考价值。

 

...

在Stack Overflow上看到了这个问题,自己翻译了过来。


 

The onload event is a standard event in the DOM, while the ready event is specific to jQuery. 

window.onload 是DOM中的标准事件,而$(document).ready() 是jQuery中的事件。 


 

The ready event occurs after the html document has been loaded, while the onload event occurs later, when all content (e.g. images) also has been loaded.

当HTML document 载入完毕后,ready 事件发生。但是,onload 事件发生的比ready 更晚。当网页上所有的内容(content)载入完毕后(包括图片),onload 事件才会发生。


 

The purpose of the ready event is that it should occur as early as possible after the document has loaded, so that code that adds functionality to the elements in the page doesn‘t have to wait for all content to load.

ready 事件的目的是:在document 装在完成之后,ready 事件应该尽可能早的发生。以便于给elements 添加功能,而不用等所有的content 都装载完成。


 

 

 

 

 

...

以上是关于前端window.onload 和 $(document).ready() 的区别的主要内容,如果未能解决你的问题,请参考以下文章

前端window.onload 和 $(document).ready() 的区别

jquery $(document).ready() 与window.onload的区别

window.onload 不触发的解决办法

window.onload和window.document.readystate的探究

Web前端面试题

window.onload