Sha*_*e N 7 javascript jquery javascript-events
如何清除设置为通过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>
Run Code Online (Sandbox Code Playgroud)
无论我试图绕过它,警报都会发生.有没有办法做到这一点?
如果你描述了你真正试图解决的问题,你可能会得到最合适的答案.
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;
Run Code Online (Sandbox Code Playgroud)
或者,如果你想破解jQuery(超出文档化的接口),你可以这样做:
$(document).ready(function() {
alert("ready");
});
// stop the ready handler
$.isReady = true;
Run Code Online (Sandbox Code Playgroud)
你可以在这里看到最后一个工作:http://jsfiddle.net/jfriend00/ZjH2k/.这是有效的,因为jQuery使用属性:$.isReady跟踪它是否已经触发了现成的处理程序.将它设置为true会使它认为它已经解雇了它,因此它不会再次执行它.