您可以从冒泡到正文的元素中删除 $.on() 添加的事件侦听器吗?
Can you remove an event listener added by $.on() from an element bubbling up to the body?
鉴于这样的情况 $('body').on('click', 'a', someReference);
是否有任何方法可以使用 $.off()
删除特定 <a>
的此侦听器?
... is there any way to remove this listener for a specific <a>
using $.off()
?
没有。仅仅是因为没有处理程序附加到任何 <a>
。您正在使用 事件委托 。处理程序附加到 <body>
并且仅当它源自 <a>
.
时才执行
但是您可以将事件处理程序绑定到停止事件传播的特定 a
:
$(specific_a).on('click', false);
$('body').on('click', 'a', function(){
console.log($(this).text());
});
$('a').eq(1).on('click', false);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#">normal</a>
<a href="#">stopped</a>
鉴于这样的情况 $('body').on('click', 'a', someReference);
是否有任何方法可以使用 $.off()
删除特定 <a>
的此侦听器?
... is there any way to remove this listener for a specific
<a>
using$.off()
?
没有。仅仅是因为没有处理程序附加到任何 <a>
。您正在使用 事件委托 。处理程序附加到 <body>
并且仅当它源自 <a>
.
但是您可以将事件处理程序绑定到停止事件传播的特定 a
:
$(specific_a).on('click', false);
$('body').on('click', 'a', function(){
console.log($(this).text());
});
$('a').eq(1).on('click', false);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#">normal</a>
<a href="#">stopped</a>