清除 jquery document.ready() 调用

发布于 2024-12-10 09:46:06 字数 434 浏览 0 评论 0原文

如何清除设置为通过 jQuery document.ready() 调用触发的匿名函数?

例如:

<script type="text/javascript">
    //some code sets a doc ready callback
    $(document).ready(function ()
    {
        alert('ready');
    });
    
    //my attempt to prevent the callback from happening
    window.onload = null;
    $(document).unbind("ready");
    
</script>

无论我如何尝试规避警报,警报都会发生。有什么办法可以做到这一点吗?

How do I clear out anonymous functions that are set to trigger via a jQuery document.ready() call?

For example:

<script type="text/javascript">
    //some code sets a doc ready callback
    $(document).ready(function ()
    {
        alert('ready');
    });
    
    //my attempt to prevent the callback from happening
    window.onload = null;
    $(document).unbind("ready");
    
</script>

The alert happens regardless of my attempts to circumvent it. Is there any way to do this?

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

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

发布评论

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

评论(4

心的憧憬 2024-12-17 09:46:06

如果您描述了您真正想要解决的问题,您可能会得到最合适的答案。

jQuery 没有公开记录的方法来撤消或阻止 document.ready() 处理程序。如果您控制代码,您可以使用全局变量和条件,如下所示:

var skipReady = false;
$(document).ready(function ()
{
    if (!skipReady) {
        alert('ready');
    }
});

// skip the document.ready code, if it hasn't already fired
skipReady = true;

或者,如果您想稍微侵入一下 jQuery(超出已记录的接口),您可以执行以下操作:

$(document).ready(function() {
    alert("ready");
});

// stop the ready handler
$.isReady = true;

您可以在此处查看最后一项工作: http://jsfiddle.net/jfriend00/ZjH2k/。这是有效的,因为 jQuery 使用属性:$.isReady 来跟踪它是否已经触发了就绪处理程序。将其设置为 true 会使其认为它已经解雇了它们,因此不会再次执行此操作。

You'd probably get the most appropriate answer if you described what problem you're really trying to solve.

jQuery doesn't have a publicly documented way to undo or block document.ready() handlers. If you control the code, you can use a global variable and a conditional like this:

var skipReady = false;
$(document).ready(function ()
{
    if (!skipReady) {
        alert('ready');
    }
});

// skip the document.ready code, if it hasn't already fired
skipReady = true;

Or, if you want to hack into jQuery a bit (beyond the documented interfaces), you can do this:

$(document).ready(function() {
    alert("ready");
});

// stop the ready handler
$.isReady = true;

You can see this last one work here: http://jsfiddle.net/jfriend00/ZjH2k/. This works because jQuery uses the property: $.isReady to keep track of whether it has already fired the ready handlers or not. Setting it to true makes it think it has already fired them so it won't every do it again.

清君侧 2024-12-17 09:46:06

这有效:

$(document).bind("ready", function () { alert("hey!"); });
$(document).unbind("ready");

对我来说似乎是一个错误 - jQuery 中的所有其他事件都可以解除绑定。省略这一点是不一致的。

不是对遗漏的直接答案,但这里有一些来自 jQuery 文档 的相关信息:

以下所有三种语法都是等效的:

  • $(文档).ready(处理程序)
  • $().ready(handler)(不推荐这样做)
  • $(处理程序)

还有$(document).bind("ready", handler)。其行为与ready方法类似,但有一个例外:如果ready事件已经触发并且您尝试.bind("ready"),则绑定处理程序将不会被执行。以这种方式绑定的就绪处理程序将在上述其他三种方法绑定之后执行。

This works:

$(document).bind("ready", function () { alert("hey!"); });
$(document).unbind("ready");

Seems like a bug to me - all other events in jQuery are able to be unbound. Omitting this one is inconsistent.

Not a direct answer as to the omission, but here's some related info from jQuery docs:

All three of the following syntaxes are equivalent:

  • $(document).ready(handler)
  • $().ready(handler) (this is not recommended)
  • $(handler)

There is also $(document).bind("ready", handler). This behaves similarly to the ready method but with one exception: If the ready event has already fired and you try to .bind("ready") the bound handler will not be executed. Ready handlers bound this way are executed after any bound by the other three methods above.

二智少女 2024-12-17 09:46:06

超级老问题,但最近遇到需要这样做,以防止我无法控制的 document.ready 代码在某些情况下运行。这可以通过代理 jQuery 的 Ready 函数来实现,就像一个测试间谍一样。以下内容将起作用:

var ready = $.prototype.ready;

// proxy the ready function
$.prototype.ready = function ( fn, allowed ) {
    allowed = allowed || false;

    if ( allowed ) {
        ready.call( this, fn );
    }
};

所有对 $( document ).ready 的调用现在都将被忽略。您可以通过传递 true 作为第二个参数来覆盖此行为:$( document ).ready( fn, true )

Super old question, but came across the need to do this recently to prevent document.ready code I didn't control from running in certain instances. This can be achieved by proxying jQuery's ready function, rather like a test spy. The following will work:

var ready = $.prototype.ready;

// proxy the ready function
$.prototype.ready = function ( fn, allowed ) {
    allowed = allowed || false;

    if ( allowed ) {
        ready.call( this, fn );
    }
};

All calls to $( document ).ready will now be ignored. You can override this behaviour by passing true as the second argument: $( document ).ready( fn, true )

久而酒知 2024-12-17 09:46:06

$(document).ready() 依赖于由浏览器触发的 onLoad 事件,这意味着您无法阻止它的发生。如果alert()是由某些条件确定的,那么我将使用if/else语句来决定是否调用它。

$(document).ready() is dependent on the onLoad event which is triggered by the browser meaning you can not prevent it from happening. If the alert() is determined by some condition then I would use an if/else statement to decide whether it is called.

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