如何在jQuery中触发$().ready()? [英] How to trigger $().ready() in jQuery?

查看:57
本文介绍了如何在jQuery中触发$().ready()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jQuery,是否有办法在页面加载后的某个时刻重新触发document.ready触发器?

Using jQuery, is there a way to retrigger the document.ready trigger at some point after a page has loaded?

更新: jQuery在它们运行后就将其丢弃.在我的实验中,一旦初始ready事件被触发,jQuery就会立即遇到().ready回调.

Update: jQuery sheds them once they are run. In my experiments it looks like jQuery will run a ().ready callback as soon as it is encountered once the initial ready event is triggered.

推荐答案

如果只需要确保运行新的初始化函数,则无需执行任何操作.在$().ready中添加新的事件处理程序时,文档加载完成后,它将立即运行.

If you just need to make sure that new initialization functions get run, you don't need to do anything. When you add a new event handler in $().ready, after the document finishes loading, it will run immediately.

显然,直接调用$().trigger("ready")不起作用,因为jQuery在运行就绪处理程序后会重置它们.您需要跟踪需要调用的函数,并按如下所示直接调用它们.

Apparently, calling $().trigger("ready") directly doesn't work because jQuery resets the ready event handlers after it runs them. You will need to keep track of the functions you need to call, and call them directly as below.

当然,最好直接调用该函数,这样您就不必重新运行您不想要的东西(某些插件可能有自己的$().ready()函数)不会运行两次).

Of course, it might be better to just call the function directly, so you don't re-run something you didn't intend to (some plugins might have their own $().ready() functions that they don't expect to run twice).

$().ready(initializationFunction);

// later:
initializationFunction(jQuery);

这篇关于如何在jQuery中触发$().ready()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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