如何清除设置为通过 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>
无论我试图规避它,警报都会发生。有什么办法吗?
最佳答案
如果您描述了您真正想要解决的问题,您可能会得到最合适的答案。
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(除了已记录的接口(interface)之外),你可以这样做:
$(document).ready(function() {
alert("ready");
});
// stop the ready handler
$.isReady = true;
您可以在这里看到最后一个作品:http://jsfiddle.net/jfriend00/ZjH2k/ .这是有效的,因为 jQuery 使用属性:$.isReady
来跟踪它是否已经触发了就绪处理程序。将其设置为 true 会使它认为它已经解雇了他们,所以它不会再这样做了。
关于javascript - 清除 jquery document.ready() 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7814408/