javascript 清除 jquery document.ready() 调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7814408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clearing a jquery document.ready() call
提问by Shane N
How do I clear out anonymous functions that are set to trigger via a jquery document.ready() call?
如何清除设置为通过 jquery document.ready() 调用触发的匿名函数?
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??
无论我试图规避它,警报都会发生。有没有办法做到这一点?
采纳答案by jfriend00
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:
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;
Or, if you want to hack into jQuery a bit (beyond the documented interfaces), you can do this:
或者,如果您想深入了解 jQuery(超出文档化的接口),您可以这样做:
$(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.
你可以在这里看到最后一个作品:http: //jsfiddle.net/jfriend00/ZjH2k/。这是有效的,因为 jQuery 使用属性:$.isReady
来跟踪它是否已经触发了就绪处理程序。将它设置为 true 使它认为它已经解雇了他们,所以它不会再次这样做。
回答by gilly3
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.
对我来说似乎是一个错误 - jQuery 中的所有其他事件都可以解除绑定。省略这一点是不一致的。
Not a direct answer as to the omission, but here's some related info from jQuery docs:
不是对遗漏的直接回答,但这里有一些来自jQuery 文档的相关信息:
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.
以下所有三种语法都是等效的:
$(document).ready(handler)
$().ready(handler)
(不推荐这样做)$(handler)
还有
$(document).bind("ready", handler)
。这与 ready 方法的行为类似,但有一个例外:如果 ready 事件已经触发并且您尝试.bind("ready")
绑定的处理程序将不会被执行。以这种方式绑定的就绪处理程序在上述其他三种方法的任何绑定之后执行。
回答by baseten
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:
超级老问题,但最近遇到需要这样做以防止我无法控制的 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 );
}
};
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 )
$( document ).ready
现在将忽略所有调用。您可以通过将 true 作为第二个参数传递来覆盖此行为:$( document ).ready( fn, true )
回答by Justin Lucas
$(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.
$(document).ready() 取决于浏览器触发的 onLoad 事件,这意味着您无法阻止它发生。如果 alert() 由某些条件确定,那么我将使用 if/else 语句来决定是否调用它。