jquery $(document).ready() 队列顺序

发布于 2024-09-28 03:01:24 字数 272 浏览 3 评论 0原文

我有一个页面index.html,它扩展了base.html(django),并且每个页面都定义了$(document).ready。现在,我期望 base.html 中的那个先行,但我错了,index 中的那个是第一个,而 base.html 中的那个则在后面开始。我猜这是因为索引在基础之前完成加载,而不是 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活泼老夫 2024-10-05 03:01:24

理论上,您可以通过操作 jQuery.readyList 数组并移动内容来更改此设置,但您可能会有点盲目。相反,我建议将需要在其他脚本之后运行的脚本从 document.ready 切换到 window.onload,如下所示:

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

看来您无论如何都想要这个,因为您正在处理窗口大小和缩放,并且这种情况发生在加载图像之后,所以您的缩放将是正确的,不会仍然发生变化。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文