jquery $ (document). Ready () Introduction to the use of _jquery

Source: Internet
Author: User
The 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 () will be loaded immediately after the DOM completes loading, and before the page content is loaded.
If you want a event to work on your page, you are should call it inside the $ (document). Ready () function. Everything inside it'll load as soon as Thedom is loaded and before the page contents are.
Copy Code code as follows:

$ (document). Ready (function () {
Put all your jQuery goodness in.
});

There are many ways to ensure that events work correctly on the page, $ (document). Ready () has an advantage over other methods. First, you don't have to put any "behavioral" tags on the HTML. In addition, you can write javascript/jquery to a separate JS file, which is easy to maintain, but also to ensure that the content of JS and page isolation. If you are more careful when browsing the web, you will often see this: when you hover your mouse over a connection, sometimes the status bar displays a message like "javascript:void ()". This is how you put an event directly into the <a href> label.

In some pages that use traditional JavaScript, you will see the "onload" attribute in the <body> tab. This leads to a problem: it defines that there can be only one function event on the body. Yes, because it adds the "behavioral" label to the content. If you want to solve this problem, please refer to Jeremy Keith's book: DOM scripting, which tells how to create a "addloadevent" function in a separate JS file that allows multiple functions to be loaded in the body. But this approach needs to write a significant amount of code for an inherently simple problem, and it triggers these events when the window is loaded, which makes me think of $ (document) again. The benefits of Ready ().

Use $ (document). Ready (), you can allow your event to load or trigger before the window loads. All you write in this method are ready to load or trigger at the earliest moment. That is, once the DOM is registered in the browser, the code in the $ (document). Ready () begins to execute. This allows the user to see the page element at the first glance, and the effect can be run.

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.