简体   繁体   English

如何防止点击事件传播到父元素

[英]How to prevent click event to propagate to parent element

Can't really explain it so I made a jsfiddle but as you can see when you open the menu by clicking the toggleButton and you click the div removeBtn and that removes the item but the slide toggle is activated because of it... 不能真正解释它,所以我做了一个jsfiddle,但是正如您看到的那样,当您通过单击toggleButton并单击div removeBtn来打开菜单时,该菜单将删除该项目,但由于此原因而激活了滑动开关...

I'm not using the conventional click slidetoggle because I'd like the mainMenu to close if the user clicks outside of the div... 我没有使用传统的点击的slideToggle,因为我喜欢mainMenu关闭,如果用户点击的div以外...

Any idea why? 知道为什么吗?

Use e.stopPropagation(); 使用e.stopPropagation(); to prevent propagation of the click event : 以防止click事件的传播:

$('#removeBtn').click(function(e) {
    e.stopPropagation();
    $(this).parent('div').remove();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM