Home  >  Article  >  Web Front-end  >  Summary of the use of $(document).ready() in jquery_jquery

Summary of the use of $(document).ready() in jquery_jquery

WBOY
WBOYOriginal
2016-05-16 17:00:081152browse

window.onload = function(){ alert("welcome"); }
The purpose of this writing method is to automatically execute the defined js code (function) after the page is loaded.

$(document).ready(function(){.... }) This function is used to replace window.onload in the page;

document.ready() is similar to the traditional method . The difference is that the onload() method occurs only after the page is loaded, including DOM elements and other pages. The loading of elements (such as images), therefore, using the document.ready() method performs faster than the onload() method.

Javascript can only perform certain operations on DOM elements after they have been defined. jQuery uses document.ready to ensure that the code to be executed is executed after the DOM element has been loaded.

For example:

Copy code The code is as follows:

< ;script type="text/javascript">
$(document).ready(function () {
alert("My first jQuery code!");
});


The meaning of this code is: when the Dom Tree is loaded, a warning message is displayed. document.ready() is similar to the traditional method , except that the onload() method occurs only after the page is loaded, including DOM elements and other page elements (such as image), therefore, the execution speed of using the document.ready() method is faster than the onload() method.

Finally two points to note:

Make sure there is no registered function in the onload event of the element, otherwise the $(document).ready() event may not be triggered. (
I tried to demonstrate this with the example below, but without success, so I guess this is just a possibility.)

Copy code The code is as follows:



My second jQuery




jQuery Simple Example 2


I would like to say:





You can use the $(document).ready() event unlimited times in the same page. The registered functions will be executed in sequence (in the code).
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn