Share $ (document). ready () jQuery

Source: Internet
Author: User

This article describes $ (document). ready () and its importance.

Address: http://www.learningjquery.com/2006/09/introducing-document-ready By Karl SwedbergThe first thing to learn about jQuery is: If you want an event to run on your page, you must call this event in $ (document). ready. All elements or events included in $ (document). ready () are loaded immediately after the DOM is loaded and before the page content is loaded.

If you want an event to work on your page, you should call it inside the $ (document ). ready () function. everything inside it will load as soon as theDOM is loaded and before the page contents are loaded.

 $(document).ready(function() {
// put all your jQuery goodness in here.
});
There are many ways to ensure that the event works normally on the page, $ (document). ready () is more advantageous than other methods. First, you do not have to place any "Behavior" mark on HTML. In addition, you can write JavaScript/jQuery into an independent js file, which is easy to maintain and ensures the isolation between js and page content. If you are more careful when browsing the Web page, you will often see this situation: When you hover your mouse over a connection, sometimes the status bar displays "javascript: void (). This is caused by placing an event directly in the <a href> label.
On some pages that use traditional JavaScript, you will see the "onload" attribute in the <body> tag. This causes a problem: it limits only one function event on the body. Yes, because it adds the "behavior" label to the content. To solve this problem, see Jeremy Keith's book DOM Scripting, which describes how to create an "addLoadEvent" function in a separate js file, it allows multiple functions to be loaded in the body. However, this method requires writing a considerable amount of code for a simple problem. In addition, this method triggers these events when loading windows, this reminds me of $ (document) again ). ready () benefits.
With $ (document). ready (), you can load or trigger your events before loading the window. All the information you wrote in this method is prepared to load or trigger at the earliest time. That is to say, once DOM is registered in the browser, the code in $ (document). ready () starts to be executed. In this way, you can run the special effect when you first see the page elements. JQuery: é crivez moins pour faire plus!

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.