This article is mainly about the $ (document) in jquery. Ready () The use of a detailed summary of the introduction, the needs of friends can come to the reference, I hope to help everyone else
Window.onload = function () {alert ("Welcome");} This is done to automate the definition of 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 traditional methods <body onload= "load ()" > Similar, and the onload () method occurs after the page is loaded, including the loading of DOM elements and other page elements (such as pictures) , the use of the Document.ready () method is faster than the OnLoad () method. Javascript You can perform some action on a DOM element only after it has been defined, jquery uses Document.ready to ensure that the code being executed is executed when the DOM element is loaded. For example: code is as follows: <script type= "Text/javascript" > $ (document). Ready (function () {alert (" My first jquery code! "); </script> This code means that when the DOM tree is loaded, a warning message is displayed. Document.ready () and traditional methods <body onload= "load ()" > Similar, and the onload () method occurs after the page load is complete, which includes the loading of DOM elements and other page elements (such as pictures), so Using the Document.ready () method is faster than the OnLoad () method. Finally note two: Ensure that there is no registration function in the OnLoad event for <body> elements, or it may not trigger $ (document). Ready () event. (I tried to illustrate the situation with the example below, but it didn't work, so I think it just might happen.) ) Code as follows: <html> <head> <title>my second jquery</title> <script type= "Text/javascript"Src="/js/jquery.js "></script> <script type=" Text/javascript ">//below is the load function containing the jquery registration function $ functions Load () { $ ("P"). Append ("<b>Hello</b>");//Below is the jquery code $ (document). Ready (function () {$ ("P" ). Append ("My first jquery code!"); $ ("P"). Append ("<b>Hello</b>"); }); </script> </head> <body onload= "load ()" > <h2>jquery Simple Example 2</h2> <p>i Say: </p> </body> </html> can use $ (document) for unlimited time on the same page. Ready () event. The functions that are registered are executed sequentially in the order in which they are (in code).