jquery $(document).ready() queue order

久未见 提交于 2020-01-05 03:00:32

问题


I have a page index.html that extends base.html (django) and each of them has a $(document).ready defined. Now, I was expecting the one in base.html to go of first, but I was wrong, the one in index is first and the one in base.html starts after. I guess this is because index finishes loading before base, and not some default behavior of jquery, to have children execute before the parent. Can I do something to change this order? Because the parent decides (depending on window size) if the menu should be vertical or horizontal, and the child scales a div to occupy the rest of the window. Now I would like the scaling to occur after I have the menu in place.


回答1:


You could theoretically change this by manipulating the jQuery.readyList array and moving things around, but you'd be poking a bit blind in there. Instead I'd recommend switching your script that needs to run after the others from document.ready to window.onload, like this:

$(window).load(function() {
  //code to run after the others
});

It seems you'd want this anyway, since you're dealing with window size and scaling, and this happens after the images are loaded, so your scale would be correct, not still changing.



来源:https://stackoverflow.com/questions/3976493/jquery-document-ready-queue-order

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