$ (document) in jquery. Ready () Usage Summary

Source: Internet
Author: User

This article is mainly about the $ (document) in jquery. Ready () using the method for a detailed summary of the introduction, the need for friends can come to the reference, I hope to have some help window.onload = function () {alert (" Welcome "); The purpose of this is to automatically execute the definition JS code (function) when the page is loaded.

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

Document.ready () and the traditional method <body onload= "load ()" > similar, unlike the OnLoad () method, which occurs only after the page is loaded, which includes the loading of DOM elements and other page elements (examples), Using the Document.ready () method executes faster than OnLoad ().

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 to be executed is executed when the DOM element is loaded.

Like what:

The code is as follows:

<script type= "Text/javascript" >

$ (document). Ready (function () {

Alert ("My first jquery code!");

});

</script>

This code means: When the DOM tree is loaded, a warning message is displayed. Document.ready () and the traditional method <body onload= "load ()" > similar, unlike the OnLoad () method, which occurs only after the page is loaded, which includes the loading of DOM elements and other page elements (examples), Using the Document.ready () method executes faster than OnLoad ().

The last two points to note:

Ensure that no function is registered in the OnLoad event of the <body> element, or the $ (document) event may not be triggered. (I tried to use the following example to illustrate the situation, but it didn't work, so I guess that's just what happens.) )

The code is as follows:

<title>my Second jquery</title>

<script type= "Text/javascript" src= "/js/jquery.js" ></script>

<script type= "Text/javascript" >//The following is a function of load that contains the jquery registration function

$ function Load () {

$ ("P"). Append ("<b>Hello</b>");

}//Below is the code for jquery

$ (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>

You can use the $ (document). Ready () event indefinitely on the same page. The functions that are registered are executed sequentially in order (in the code).

For more information, refer to: http://www.jb51.net/article/46780.htm

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.