The following are several jQuery ready () methods I know. If there are other methods, I would like to inform you
1. The most common and standard
The Code is as follows:
$ (Document). ready (){
});
2. Is the abbreviation above:
The Code is as follows:
$ (Function (){
})
Strange? Why? Didn't the document object be reADy before the function is executed? Where is document? Let's take a look at the jQuery source code:
The Code is as follows:
// JQuery constructor;
Var jQuery = function (a, c ){
// $ (Document). ready () is short for $ (function;
If (a & typeof a = "function" & jQuery. fn. ready) return jQuery (document). ready ();
// Ensure that parameter a is not null and the default value is document;
A = a | jQuery. context | document;
Yeah! Found. Let's look at the $ parameter of this method.
$ (Selector, context)
The first is the selector, and the second is the container.
If not specified, the default value is document.
3. Okay! I admit that this method is for soy sauce.
The Code is as follows:
JQuery (document). ready (function (){
});
4.
The Code is as follows:
JQuery (function ($ ){
Alert ($ ("# ready1" pai.html ());
});
There is no difference between the fourth method and the third method? Take a closer look! We passed a parameter $ to the functIOn.
The fourth method is generally used to deal with the conflict between jQuery $ and other libraries. in noConflict (), we can directly use jQuery in code instead of $, but we are used to $. What should we do? See the following code:
The Code is as follows:
JQuery. noConflict ();
JQuery (function ($ ){
Alert ($ ("# ready1" ).html (); // we can use the $ symbol again.
});
The above are several jQuery ready () methods I know. If there are other methods, please let me know.