Home  >  Article  >  Web Front-end  >  A table tells you the difference between windows.onload() and $(document).ready()_jquery

A table tells you the difference between windows.onload() and $(document).ready()_jquery

WBOY
WBOYOriginal
2016-05-16 16:48:021230browse

After the browser loads the DOM, it will add events to the DOM elements through javascript. In javascript, the window.onload() method is usually used.

In jquery, use the $(document).ready() method. Let’s introduce the difference between the two.

window.onload() $(document).ready()
Execution Timing Executed after all elements of the page (including images and referenced files) are loaded.
  window.onload() $(document).ready()
执行时机 在页面所有元素(包括图片,引用文件)加载完后执行。

页面中所有HTML DOM,CSS DOM结构加载完之后就会执行,其他图片可能没有加载完.

如果想要网页所有内容(包括图片等)加载完毕,再注册事件,使用$(window).load(function);

等价于window.onload()

编写个数

不能同时写多个,后面的将会覆盖前面的。ex:

window.onload=function(){ alert("A"); }

window.onload=function(){ alert("B"); }

结果会执行“B”

如果想要顺序执行alert("A")和alert("B")需写成

window.onload=function(){

alert("A");

alert("B");

}

可以同时写多个
简写

$(document).ready(function(){

  //to do;

});

可写成

$().ready(function(){ //$()不带参数默认是document

  //to do;

});或

$(function(){

  //to do;

});

 

All HTML DOM and CSS DOM structures in the page will be executed after loading. Other images may not be loaded. If you want all the content of the web page (including pictures, etc.) to be loaded before registering the event, use $(window).load(function); Equivalent to window.onload()
Write number You cannot write multiple ones at the same time, the later ones will overwrite the previous ones. ex: window.onload=function(){ alert("A"); } window.onload=function(){ alert("B"); } The result will be "B" If you want to execute alert("A") and alert("B") sequentially, you need to write window.onload=function(){ alert("A"); alert("B"); } You can write multiple ones at the same time
Abbreviation None $(document).ready(function(){   //to do; }); can be written as $().ready(function(){ //$() without parameters defaults to document   //to do; }); or $(function(){   //to do; });
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn