A page can't be manipulated safely until the document is "ready." Generally people write <script>
tags in the starting of the document, in the <head>
even before the <body>
is written. So, technically, if you are manipulating something from the <body>
contents, it is not present at the time of execution.
So, jQuery's $( document ).ready()
waits for the HTML Document content to be loaded fully and become ready, after rendering all the elements into the window
object or in a nutshell, completes the loading of the body.
Then whatever content present in the code is executed, once the HTML document is fully loaded, which makes sure every HTML element is present as you execute your JS code.
Check out:
And about binding two ready handlers, why do you need two? You can combine the code in a single function. You must give a minimal code to explain. I assume you have something like this:
$( document ).ready( function () {
// Code block 1 start...
alert( "First Function..." );
// Code block 1 end...
});
$( document ).ready( function () {
// Code block 2 start...
alert( "Second Function..." );
// Code block 2 end...
});
And yes, the above is possible. Also, there's no difference from the above having:
$( document ).ready( function () {
// Code block 1 start...
alert( "First Function..." );
// Code block 1 end...
// Code block 2 start...
alert( "Second Function..." );
// Code block 2 end...
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…