为什么这种解体不起作用? [英] Why this unbind doesn't work?
本文介绍了为什么这种解体不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我几分钟前尝试回答这个问题并准备好此示例我自己:
I try to answer this question a few minutes ago and prepared this example for myself :
<script>
function trialMethod()
{
alert('On Submit Run!'); return true;
}
function trialMethod2()
{
alert('On Submit Run trialMethod2!'); return true;
}
</script>
<form id="aspnetForm" onsubmit="trialMethod();">
<input type="submit">
</form>
为什么第一个解绑不起作用:
Why the first unbind doesn't work :
<input type="button" id="btnTrial1" value="UNBIND 1"
onclick="$('#aspnetForm').unbind('submit', trialMethod);">
但这个适用于trialMethod2方法:
But this one works for the trialMethod2 method :
<input type="button" id="btnTrial2" value="UNBIND 2"
onclick="$('#aspnetForm').bind('submit', trialMethod2).unbind('submit', trialMethod2);">
推荐答案
第一个解除绑定方案不起作用,因为jQuery的事件模型。 jQuery将每个事件处理函数存储在一个数组中,您可以通过$(#foo)。data('events')访问它。 unbind函数仅查找此数组中的给定函数。因此,您只能取消绑定()使用bind()
The first unbind scenario doesn't work, because of jQuery's event model. jQuery stores every event handler function in an array that you can access via $("#foo").data('events'). The unbind function looks just for given function in this array. So, you can only unbind() event handlers that were added with bind()
这篇关于为什么这种解体不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文