使用委托时如何停止事件传播? [英] How to stop event propagation when using delegate?

查看:60
本文介绍了使用委托时如何停止事件传播?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用.bind将事件绑定到子代和父代上时,子事件可以停止事件传播,返回false;但是当我使用委托时,返回false;不会停止事件传播.

When I use .bind to bind event on child and parent, child event can stop event propogation with return false; But when I use delegate, return false; does not stop event propagation.

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().这是一个小提琴

e.stopPropagation() won't work in this case. Use e.stopImmediatePropagation() instead. Here's a fiddle

这篇关于使用委托时如何停止事件传播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆