防止jQuery就绪处理程序触发 [英] Prevent jQuery ready handlers from firing

查看:63
本文介绍了防止jQuery就绪处理程序触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,在某种情况下,我想要任何jQuery ready 处理程序已经绑定不会实际触发DOMContentReady事件。这些处理程序绑定在各个地方,包括各种包含文件,插件等。因此,虽然我可以在靠近页面顶部的脚本中设置某种全局变量,然后在条件下放置一个条件来检查每个<$ c前面的变量$ c> $(文件).ready()绑定,我宁愿没有。

I have a situation where in a certain condition, I want whatever jQuery ready handlers have been bound not to actually fire on the DOMContentReady event. These handlers are bound in various places, including various include files, plugins and so on. So while I could set some kind of global variable in a script near the top of my page, and put an if condition to check for this variable in front of every $(document).ready() binding, I'd rather not.

理想情况下我正在寻找两个中的一个事情,

Ideally I'm looking for one of two things,


  • 我可以在包含之前在jQuery对象上设置的属性,告诉它忽略对的调用.ready()

  • 我可以在DOMContentReady触发之前在内联脚本中调用文档末尾的方法取消绑定 ready()处理程序。

  • a property I could set on the jQuery object before the includes that would tell it to ignore calls to .ready()
  • a method I could call towards the end of the document, in an inline script before DOMContentReady has fired, to unbind the ready() handlers.

显而易见的一个, $(文件).unbind('ready')不这样做,我猜是因为 .ready()不是普通的事件绑定。

The obvious one, $(document).unbind('ready') doesn't do it, I guess because .ready() isn't an ordinary event binding.

可以这样做吗?如果是这样,怎么样?

Can this be done? If so, how?

我有一个模糊的想法是扩展jQuery原型,并替换 ready()使用一种方法来检查我将要介绍的新属性。这有意义吗?

One vague idea I had would be to extend the jQuery prototype, and replace ready() with a method that would check a new property that I would introduce. Does that make sense?

推荐答案

你应该使用 holdready

$.holdReady(true);
$.getScript("myplugin.js", function() {
  $.holdReady(false);
});  

这篇关于防止jQuery就绪处理程序触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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