清除jquery document.ready()调用 [英] Clearing a jquery document.ready() call

查看:482
本文介绍了清除jquery document.ready()调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

例如:

<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??

推荐答案

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

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

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

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;

或者,如果你想破解jQuery(超出文档化的界面),你可以做这个:

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;

你可以在这里看到最后一个工作: http://jsfiddle.net/jfriend00/ZjH2k/ 。这是因为jQuery使用属性: $。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.

这篇关于清除jquery document.ready()调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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