我怎样才能让 jQuery “准备好”?事件处理程序在所有其他事件处理程序之后触发?
我正在编写一个 Greasemonkey 脚本,该脚本实际上会升级页面上使用的 jQuery 版本。为此,我需要添加一个“就绪”事件处理程序,该处理程序将在页面上可能存在的所有其他事件处理程序之后触发。
我知道 jQuery 在调用就绪事件处理程序之前会等待 DOM 可操作,那么有没有办法影响它执行它们的顺序?谢谢你,
I am working on a Greasemonkey script that will actually upgrade the version of jQuery used on the page. To do this, I need to add a "ready" event handler that will fire after all the other ones that might be on the page.
I know that jQuery waits for the DOM to be manipulable before invoking the ready event handlers, so is there a way to influence the order in which it executes them? Thank you,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
他们按照注册的顺序被调用。所以从页面顶部到底部。如果您需要这是最后一个注册的就绪回调,请将其注册在 body 标记的最末尾。还可以使用
$(window).load
而不是$(document).ready
。They are called in the order they are registered. So from the top of the page to the bottom. If you need this to be the last registered ready callback register it at the very end of the body tag. Also use the
$(window).load
as opposed to$(document).ready
.就绪处理程序被添加到一个
readyList
数组中,我很确定它是私有的,所以我认为您无法直接影响它。您也许可以做的一件事是将代码添加到
readyList
中,但将其放在setTimeout()
中,以便它等待一段时间才能执行。希望所有其他的事情都能先完成。不过,像这样升级 jQuery 时您可能会遇到麻烦。例如,存储事件处理程序和其他数据的
jQuery.cache
的实现可能存在差异。因此,如果
jQuery.cache
填充了一个版本,它可能与另一个版本不兼容。The ready handlers are added to a
readyList
Array, which I'm pretty sure is private, so I don't think you'll be able to influence it directly.One thing you could perhaps do is add your code to the
readyList
, but place it in asetTimeout()
so it waits a bit to execute. Hopefully all the others will be done first.Still, you may have troubles when upgrading jQuery like this. For example, there may be differences in the implementation of
jQuery.cache
which stores event handlers, and other data.So if
jQuery.cache
was populated with one version, it may not be compatible with another.如何控制顺序在 jQuery $(document).ready 中调用的函数
根据上述问题的答案,它们应该按照添加的顺序触发(该特定问题中的 ajax 调用向水中添加的泥浆比在你的问题)。
How to control the order of functions being called in jQuery $(document).ready
According to answers given to the question above, they should fire in the order they are added (the ajax-calls in that specific question add more mud to the water than in your question).