The function of $(document).ready() in Jquery is similar to the window.onload method in traditional JavaScript, but it is still different from the window.onload method.
1. Execution time
window.onload must wait until all elements in the page, including images, are loaded before it can be executed.
$(document).ready() is executed after the DOM structure is drawn, without having to wait until it is loaded.
2. The number of writings is different
You cannot write multiple window.onload methods at the same time. If there are multiple window.onload methods, only one will be executed
$(document).ready() can be written multiple times at the same time, and all can be executed
3. Simplified writing
window.onload has no simplified writing method
$(document).ready(function(){}) can be abbreviated as $(function(){});
In my previous development, I usually used JavaScript, and I always used jquery mode. That is, most of the time, the first line is:
At this time, you don’t have to wait for all js and images to be loaded before you can execute some methods, but sometimes, you have to wait for all
When all elements are loaded, some methods can be executed. For example, some pictures or other aspects have not been loaded yet. At this time, clicking some buttons will lead to unexpected situations. At this time,
Need to use:
});
Summary and comparison: