After the browser loads the DOM, it will add events to the DOM element through javascript. In javascript, the window.onload() method is usually used.
In jquery, the $(document).ready() method is used. Let me introduce the difference between the two below.
| window.onload() | $(document).ready() | |
| Execution timing | Execute all elements of the page (including pictures and reference files) after loading. | All HTML DOMs in the page will be executed after the CSS DOM structure is loaded, and other images may not be loaded. If you want all the content of the web page (including pictures, etc.) to load, register the event and use $(window).load(function); Equivalent to window.onload() |
| Write a number | You cannot write multiple words at the same time, the latter will overwrite the previous one. 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") in sequence, you need to write it window.onload=function(){ alert("A"); alert("B"); } | Can write multiple simultaneously |
| Abbreviation | none | $(document).ready(function(){ //to do; }); Can be written as $().ready(function(){ //$() without parameters is document //to do; });or $(function(){ //to do; }); |