The following are several ways of writing ready () in jQuery that I know so far. If there are other ways of writing, please let me know
1. The most commonly used and standard The
code is as follows:
$(document).ready(){
});
2. It is the abbreviation of the above:
The code is as follows:
$(function(){
})
Very strange? Why is this possible? Isn't it to determine whether the documentobject is reADy and then execute the function? Where did the document go? Let's take a look at the source code of jQuery:
The code is as follows:
// jQuery’s constructor ;
var jQuery = function( a, c ) {
// The short form of $(document).ready(), only It 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 look at the parameters of this method
$(selector, context)
The first one is the selector, the second one is the container
If it is not filled in, it will default to document
3.Okay! I admit that this method is just for fun
The code is as follows:
jQuery(document).ready(function(){
});
4.
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
The fourth method is generally used when dealing with conflicts between jQuery's $ and other libraries. Through the jQuery.noConflict() method, we can directly pass jQuery in the code. What should I do if I use it instead of $, but I am used to using $? Look at the code below:
The code is as follows:
jQuery.noConflict();
jQuery(function ($){
alert($("#ready1").html()); //We can use the $ symbol again
});
The above is the detailed content of Summarize several implementation methods of jquery ready(). For more information, please follow other related articles on the PHP Chinese website!