Can there be more than one jQuery ready event on a page or in .js script?

独自空忆成欢 提交于 2019-12-10 17:55:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!