Javascript can perform some operation on a DOM element only after the DOM element is defined. jquery usesDocument. Ready
To ensure thatCodeIt is executed when the DOM element is loaded.
The following jquery code:
<SCRIPT type = "text/JavaScript">
$ (Document). Ready (function (){
Alert ("My first jquery code! ");
});
</SCRIPT>
This Code indicates that a warning message is displayed after the DOM tree is loaded.Document. Ready ()
And traditional methods<Body onload = "load ()">
Similar, the difference isOnload ()
This occurs only after the page is loaded. This includes loading DOM elements and other page elements (example). Therefore,UseDocument. Ready ()
Method execution speed ratioOnload ()
.
Ready (FN) description mentioned in the jquery document
Bind a function to be executed when the Dom is ready for query and operation.
This is the most important function in the event module, because it can greatly improve web applications.Program.
Simply put, this method is purely an alternative to registering events with the window. Load event. By using this method, you can call the function that you bind immediately when the Dom is ready to read and manipulate, and 99.99% of JavaScript functions need to be executed at that moment.
There is a parameter -- a reference to the jquery function -- passed to this ready event processing function. You can give this parameter any name, so you can use the $ alias without worrying about name conflicts.
Two points of attention are mentioned in the jquery document.
1. Make sure that no function is registered in the onload event of the <body> element; otherwise, the $ (document). Ready () event may not be triggered.
2. It can be used infinitely on the same page$ (Document). Ready ()
Event. The registered functions are executed sequentially.