Summary of Several implementation methods of jqueryready () _ jquery

Source: Internet
Author: User
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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.