清除 jquery document.ready() 调用
如何清除设置为通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您描述了您真正想要解决的问题,您可能会得到最合适的答案。
jQuery 没有公开记录的方法来撤消或阻止 document.ready() 处理程序。如果您控制代码,您可以使用全局变量和条件,如下所示:
或者,如果您想稍微侵入一下 jQuery(超出已记录的接口),您可以执行以下操作:
您可以在此处查看最后一项工作: 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:
Or, if you want to hack into jQuery a bit (beyond the documented interfaces), you can do this:
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 中的所有其他事件都可以解除绑定。省略这一点是不一致的。
不是对遗漏的直接答案,但这里有一些来自 jQuery 文档 的相关信息:
This works:
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:
超级老问题,但最近遇到需要这样做,以防止我无法控制的 document.ready 代码在某些情况下运行。这可以通过代理 jQuery 的 Ready 函数来实现,就像一个测试间谍一样。以下内容将起作用:
所有对
$( 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:
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() 依赖于由浏览器触发的 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.