This article is mainly about the $ (document) in jquery. Ready () using the method for a detailed summary of the introduction, the need for friends can come to the reference, I hope to have some help window.onload = function () {alert (" Welcome "); The purpose of this is to automatically execute the definition 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 the traditional method <body onload= "load ()" > similar, unlike the OnLoad () method, which occurs only after the page is loaded, which includes the loading of DOM elements and other page elements (examples), Using the Document.ready () method executes faster than OnLoad ().
Javascript can perform some action on a DOM element only after it has been defined, and jquery uses Document.ready to ensure that the code to be executed is executed when the DOM element is loaded.
Like what:
The code is as follows:
<script type= "Text/javascript" >
$ (document). Ready (function () {
Alert ("My first jquery code!");
});
</script>
This code means: When the DOM tree is loaded, a warning message is displayed. Document.ready () and the traditional method <body onload= "load ()" > similar, unlike the OnLoad () method, which occurs only after the page is loaded, which includes the loading of DOM elements and other page elements (examples), Using the Document.ready () method executes faster than OnLoad ().
The last two points to note:
Ensure that no function is registered in the OnLoad event of the <body> element, or the $ (document) event may not be triggered. (I tried to use the following example to illustrate the situation, but it didn't work, so I guess that's just what happens.) )
The code is as follows:
<title>my Second jquery</title>
<script type= "Text/javascript" src= "/js/jquery.js" ></script>
<script type= "Text/javascript" >//The following is a function of load that contains the jquery registration function
$ function Load () {
$ ("P"). Append ("<b>Hello</b>");
}//Below is the code for jquery
$ (document). Ready (function () {
$ ("P"). Append ("My first jquery code!");
$ ("P"). Append ("<b>Hello</b>");
});
</script>
<body onload= "Load ()" >
<p>i would like to say: </p>
</body>
You can use the $ (document). Ready () event indefinitely on the same page. The functions that are registered are executed sequentially in order (in the code).
For more information, refer to: http://www.jb51.net/article/46780.htm