jquery $ (document). Ready () method and Window.onload method
In most cases, the jquery code is written that requires us to put the jquery code in the
$ (document). Ready (function () {
... jquery code ........ .....
});
The above code and
$ (). Ready (function () {
... jquery code ........ .....
});
$ (function () {
... jquery code ........ .....
});
is equivalent ~ ~ The last way is the most concise way, most of the cases I use the last one, hehe ~ ~ Good remember!
Note: Sometimes our own jquery code has a baffling problem, seemingly normal code, but it is in the run-time error, then please check your code is not placed in the $ (document). Ready () method runs.
Let's say $ (document). The difference between the ready () method and the Window.onload method:
Their main difference has two points:
Timing of execution
The Window.onload method is not executed until all elements in the Web page, including all associated files of the element, are fully loaded into the browser. The event handler that is registered through the $ (document). Ready () method in jquery can be invoked as long as the DOM is fully ready, such as a picture as long as the tag is complete, without waiting for the picture to load. You can set a picture's width-high properties or styles, and so on.
$ (document). The Ready () method can be used multiple times to register a different event handler, and Window.onload can only save a reference to one function at a time, and a multiple-bound function will only overwrite the preceding function.
Let's look at the results of the Window.onload method registering two times on a page:
function One () {
Alert ("one");
}
function two () {
Alert ("two");
}
Window.onload = two;
Window.onload = one;
When the above code is run, "one" pops up.
Let's take a look at $ (document). Ready () method is called two times what the result will be.
function One () {
Alert ("one");
}
function two () {
Alert ("two");
}
$ (document). Ready (function () {
One ();
});
$ (document). Ready (function () {
Two ();
});
When the above code runs, it pops up "one" and "two" respectively.