Home >Web Front-end >JS Tutorial >Summary of several implementation methods of jquery ready()_jquery

Summary of several implementation methods of jquery ready()_jquery

WBOY
WBOYOriginal
2016-05-16 18:25:001189browse
1. The most commonly used and standard
Copy code The code is as follows:
$ (document).ready(){
});

2. It is the abbreviation of the above:
Copy code The code is as follows:
$(function(){
})

Strange? Why is this possible? Isn’t it necessary to determine whether the document object is reADy and then execute the function? Where does the document go? Let’s take a look at the source code of jQuery:
Copy code The code is as follows:

// Constructor of jQuery;
var jQuery = function( a, c ) {
// Short form of $(document).ready() , will only be executed under $(function(){...});
if ( a && typeof a == "function" && jQuery.fn.ready ) return jQuery(document).ready(a) ;
// Make sure parameter a is not empty, the default value is document;
a = a || jQuery.context || document;

Yeah! Found it, let’s take a look at the parameters of this method
$(selector, context)
The first one is the selector, and the second one is the container
If not filled in, it will default to document
3.Okay! I admit that this method is just for fun
Copy the code The code is as follows:
jQuery(document ).ready(function(){
});

4.
Copy code The code is as follows:
jQuery(function($){
alert($("#ready1").html());
});

There is no difference between the fourth method and the third method? Dear guests, please look carefully! We passed a parameter $ to functION What should I do if I want to use it instead of $, but I am used to using $? Look at the following code:

Copy the code The code is as follows:
jQuery.noConflict();
jQuery(function($){
alert($("#ready1").html()); //We can use the $ sign again
});

The above are several ways of writing jQuery’s ready () that I know so far. If there are other ways of writing, please let me know.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn