jquery – 如何在使用委托时停止事件传播?

前端之家收集整理的这篇文章主要介绍了jquery – 如何在使用委托时停止事件传播?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我使用.bind绑定子对象和父对象的事件时,子事件可以通过返回false来停止事件传播;但是当我使用委托时,返回false;不会停止事件传播.

http://jsfiddle.net/J3EAQ/

HTML:

<div class="parent">
    <div class="child"></div>
    <div class="child"></div>
</div>

JS:

$('.parent').delegate('.child','click',function(e){
    alert('child click');
    return false;
});
$('.parent').bind('click',function(e){
    alert('parent click');
});

解决方法

e.stopPropagation()在这种情况下不起作用.请改用 e.stopImmediatePropagation().这是 fiddle
原文链接:https://www.f2er.com/jquery/177454.html

猜你在找的jQuery相关文章