The first thing to learn about jquery is that if you want an event to run on your page, you must call this event in the (document). Ready (). All elements or events that are included in the. Ready () will load immediately after the DOM finishes loading and before the page content is loaded.
If you want a event to work on your page, you should call it inside the $ (document). Ready () function. Everything inside it would load as soon as Thedom is loaded and before the page contents is loaded.
. The code is as follows:
$ (document). Ready (function () {
Put all your jQuery goodness on here.
});
There are many ways to ensure that events work on the page, and $ (document). Ready () is more advantageous than 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, so that it is easy to maintain, but also to ensure that JS and page content isolation. If you're more careful when browsing the web, you'll often see this: when you hover your mouse over a connection, sometimes a message like "javascript:void ()" appears in the status bar. This is what you put an event directly into the <a href> tag.
On some pages that use traditional JavaScript, you'll see the "onload" attribute in the <body> tab. This can cause a problem: it restricts only one function event on the body. Yes, because it adds the "behavioral" label to the content. If you want to solve this problem, refer to Jeremy Keith's book: DOM Scripting, which describes how to create a "addloadevent" function in a separate JS file, which allows multiple functions to be loaded in the body. But this method needs to write a considerable amount of code for a very simple question, and this method triggers these events when the window is loaded, which has to remind me again of the benefits of Ready ().
Using the $ (document). Ready (), you can let your event load or trigger before the window loads. All you write in this method is ready to load or trigger at the earliest moment. That is, once the DOM is registered in the browser, the code in the Ready () is executed. This allows the user to see the page element at the first glance, and the effect is ready to run.
Article from: http://www.poluoluo.com/jzxy/201204/162468.html
jquery's $ (document). Ready () Use introduction