问题
I noticed that the jQuery ready event and function are used only once in most of my javascript. However, I was wandering if it is ok to use the ready event more than once. For example, is it ok to call functions in a ready event function call in my main html code, that then calls functions and events inside another ready event function call that is in my .js file?
回答1:
Yes this is completely acceptable:
$(document).ready(function() {
// do this
}):
// some other js code
$(document).ready(function() {
// do something else
}):
In this case, when the dom is ready, code inside both (or as many ready
as you have) will be executed.
回答2:
The ready event can be used more than once but you might run into issues with the order in which they run if your ready event handlers depend on each other.
来源:https://stackoverflow.com/questions/7395843/can-there-be-more-than-one-jquery-ready-event-on-a-page-or-in-js-script