Jquery basics-document. Ready

Source: Internet
Author: User

Javascript can perform some operation on a DOM element only after the DOM element is defined. jquery usesDocument. ReadyTo ensure thatCodeIt is executed when the DOM element is loaded.

The following jquery code:

 
<SCRIPT type = "text/JavaScript">
$ (Document). Ready (function (){
Alert ("My first jquery code! ");
});
</SCRIPT>

This Code indicates that a warning message is displayed after the DOM tree is loaded.Document. Ready ()And traditional methods<Body onload = "load ()">Similar, the difference isOnload ()This occurs only after the page is loaded. This includes loading DOM elements and other page elements (example). Therefore,UseDocument. Ready ()Method execution speed ratioOnload ().

Ready (FN) description mentioned in the jquery document

Bind a function to be executed when the Dom is ready for query and operation.

This is the most important function in the event module, because it can greatly improve web applications.Program.

Simply put, this method is purely an alternative to registering events with the window. Load event. By using this method, you can call the function that you bind immediately when the Dom is ready to read and manipulate, and 99.99% of JavaScript functions need to be executed at that moment.

There is a parameter -- a reference to the jquery function -- passed to this ready event processing function. You can give this parameter any name, so you can use the $ alias without worrying about name conflicts.

 

Two points of attention are mentioned in the jquery document.

1. Make sure that no function is registered in the onload event of the <body> element; otherwise, the $ (document). Ready () event may not be triggered.

2. It can be used infinitely on the same page$ (Document). Ready ()Event. The registered functions are executed sequentially.

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.