$ (document) in jquery. Ready () Introduction to the Use method

Source: Internet
Author: User

  This article is mainly about the $ (document) in jquery. Ready () The use of a detailed summary of the introduction, the needs of friends can come to the reference, I hope to help everyone else

Window.onload = function () {alert ("Welcome");} This is done to automate the definition of 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 traditional methods <body onload= "load ()" > Similar, and the onload () method occurs after the page is loaded, including the loading of DOM elements and other page elements (such as pictures) , the use of the Document.ready () method is faster than the OnLoad () method.     Javascript You can perform some action on a DOM element only after it has been defined, jquery uses Document.ready to ensure that the code being executed is executed when the DOM element is loaded.   For example:     code is as follows: <script type= "Text/javascript" > $ (document). Ready (function () {alert (" My first jquery code! "); </script>   This code means that when the DOM tree is loaded, 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 note two:   Ensure that there is no registration function in the OnLoad event for <body> elements, or it may not trigger $ (document). Ready () event. (I tried to illustrate the situation with the example below, but it didn't work, so I think it just might happen.) )   Code as follows: <html> <head> <title>my second jquery</title> <script type= "Text/javascript"Src="/js/jquery.js "></script> <script type=" Text/javascript ">//below is the load function containing the jquery registration function $ functions Load () {   $ ("P"). Append ("<b>Hello</b>");//Below is the jquery code $ (document). Ready (function () {$ ("P" ). Append ("My first jquery code!"); $ ("P"). Append ("<b>Hello</b>"); }); </script> </head> <body onload= "load ()" > <h2>jquery Simple Example 2</h2> <p>i Say: </p>  </body> </html>   can use $ (document) for unlimited time on the same page. Ready () event. 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.