$of document.ready of in jquery USES a summary

  • 2020-03-30 01:46:01
  • OfStack

The $(document). Ready (function () {... }) this function is used to replace window.onload in the page;

Document.ready () and traditional methods < The body onload = "load ()" > Similarly, the onload() method does not occur until the page is loaded, which includes DOM elements and other page elements (such as images), so the document.ready() method executes faster than the onload() method.

Javascript can only perform certain operations on a DOM element once it has been defined. JQuery USES document.ready to ensure that the code to be executed is executed when the DOM element has been loaded.

Such as:


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

When the Dom Tree is loaded, a warning message is displayed. Document.ready () and traditional methods < The body onload = "load ()" > Similarly, the onload() method does not occur until the page is loaded, which includes DOM elements and other page elements (such as images), so the document.ready() method executes faster than the onload() method.

Two final points to note:

Make sure the < Body> There is no registered function in the onload event of the element, otherwise the $(document).ready() event might not be triggered. (
I tried to demonstrate this with the following example, but it didn't work, so I think it's just possible.


<html>
<head>
<title>My second jQuery</title>
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript">
//Here's the function load with the jquery registry function $
function load(){
   $("p").append("<b>Hello</b>");
}
//Here's the code for jQuery
$(document).ready(function () {
$("p").append(" My first one jQuery code !");
$("p").append("<b>Hello</b>"); 
});
</script>
</head>
<body onload="load()">
<h2>jQuery  A simple example 2</h2>
<p>I would like to say: </p> 
</body>
</html>

The $(document).ready() event can be used an unlimited number of times on the same page. The registered functions are executed in sequence.


Related articles: