$ (document) in jquery. Ready () Using summary _jquery

Source: Internet
Author: User

Window.onload = function () {alert ("Welcome");}
This kind of writing function is to want after the page load, automatically execute the definition of JS code (function).

$ (document). Ready (function () {...}) This function is used to replace the window.onload in the page;

Document.ready () and traditional methods <body onload= "load ()" > Similar, and the onload () method occurs after the page load is complete, which includes the loading of DOM elements and other page elements (such as pictures), so Using the Document.ready () method is faster than the OnLoad () method.

Javascript can perform some action on a DOM element only after it has been defined, and jquery uses Document.ready to ensure that the code being executed is executed when the DOM element is loaded.

Like what:

Copy Code code as follows:

<script type= "Text/javascript" >
$ (document). Ready (function () {
Alert ("My first jquery code!");
});
</script>

This code means that when the DOM tree is finished loading, a warning message is displayed. Document.ready () and traditional methods <body onload= "load ()" > Similar, and the onload () method occurs after the page load is complete, which includes the loading of DOM elements and other page elements (such as pictures), so Using the Document.ready () method is faster than the OnLoad () method.

Finally, pay attention to two points:

Make sure that there is no registration function in the OnLoad event for the <body> element, or it may not trigger the $ (document). Ready () event. (
I tried to illustrate the situation with the example below, but it didn't work out, so I think it just might happen. )

Copy Code code as follows:

<title>my Second jquery</title>
<script type= "Text/javascript" src= "/js/jquery.js" ></script>
<script type= "Text/javascript" >
Here is the load function containing the jquery registration function $
function Load () {
$ ("P"). Append ("<b>Hello</b>");
}
Here's the jquery code.
$ (document). Ready (function () {
$ ("P"). Append ("My first jquery code!");
$ ("P"). Append ("<b>Hello</b>");
});
</script>
<body onload= "Load ()" >
<p>i would like to say: </p>
</body>

The $ (document). Ready () event can be used indefinitely on the same page. The functions that are registered are executed sequentially in the order in which they are (in code).

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.