1. The most commonly used and standard
$ (document).ready(){
});
2. It is the abbreviation of the above:
$(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:
// 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
jQuery(document ).ready(function(){
});
4.
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:
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.