jquery $(document).ready()队列顺序 [英] jquery $(document).ready() queue order

查看:153
本文介绍了jquery $(document).ready()队列顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展base.html(django)的页面index.html,每个页面都有$(document).ready定义。现在,我期待base.html中的那个首先出现,但我错了,索引中的那个是第一个,而base.html中的那个开始之后。我想这是因为索引在base之前完成加载,而不是jquery的一些默认行为,让子进程在父进程之前执行。我可以做些什么来改变这个订单吗?因为父级决定(取决于窗口大小)菜单应该是垂直还是水平,并且子级缩放div以占据窗口的其余部分。现在我希望在菜单到位后进行缩放。

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.

推荐答案

可以从理论上来说,通过操纵 jQuery.readyList 数组来改变这一点,然后移动一下,但你会在那里稍微盲目一点。相反,我建议将需要运行的脚本从 document.ready 切换到 window.onload ,像这样:

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.

这篇关于jquery $(document).ready()队列顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆