Can you have multiple $(document).ready(function(){ … }); sections?

前端 未结 11 1225
庸人自扰
庸人自扰 2020-11-22 13:21

If I have a lot of functions on startup do they all have to be under one single:

$(document).ready(function() {

or can I have multiple such

11条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 13:43

    Yes, it's perfectly ok.but avoid doing it without a reason. For example I used it to declare global site rules seperately than indivual pages when my javascript files were generated dynamically but if you just keep doing it over and over it will make it hard to read.

    Also you can not access some methods from another jQuery(function(){}); call so that's another reason you don't wanna do that.

    With the old window.onload though you will replace the old one every time you specified a function.

提交回复
热议问题