Difference between $(function(){}) and $(document).ready(function(){})

The difference between document.ready and onload - JavaScript document loading completion event
There are two events when the page is loaded
One is ready, indicating that the document structure has been loaded (excluding non-text media files such as pictures)
The second is onload, which indicates that all elements of the page, including pictures and other files, are loaded.
 
Many people who use jQ start writing scripts like this:
$(function(){
// do something
});
In fact, this is a shorthand for jq ready(), which is equivalent to:

$(document).ready(function(){

//do something
})
//Or the following method, the default parameter of jQuery is: "document";

$().ready(function(){

//do something
})
This is the method of jq ready(), which is Dom Ready. Its function or meaning is: after the DOM is loaded, the DOM can be manipulated.
In general, the first page response loading sequence is: domain name resolution-loading html-loading js and css-loading pictures and other information.
Then Dom Ready should be between "loading js and css" and "loading pictures and other information", and you can operate Dom.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324996864&siteId=291194637