简体   繁体   English

反应停止事件传播而不取消事件

[英]React stop event propagation without cancelling the event

I have a dropdown list (or button with split) which does its thing on a click.我有一个下拉列表(或带有拆分的按钮),只需点击一下即可。 It's inside a div which has some specific onClick behavior.它在一个 div 里面,它有一些特定的 onClick 行为。 How to make the click on the dropdown not being propagated to the div but also the default behaviour (opening the dropdown) not cancelled?如何使对下拉菜单的点击不传播到 div 并且不取消默认行为(打开下拉菜单)?

onClick = (e) => {
   e.stopPropagation();
   // continue with the original event on e.target 
   // ???
}

I resolved the problem for the moment by doing e.stopPropagation() on an intermediate div.我暂时通过在中间 div 上执行e.stopPropagation()解决了这个问题。 So if I have div1 > div2 > div3 and I want to have onClick with default behavior on div3 but avoid it firing on div1, I just added onClick={(e) => {e.stopPropagation(); e.preventDefault();}因此,如果我有 div1 > div2 > div3 并且我希望 onClick 在 div3 上具有默认行为但避免它在 div1 上触发,我只需添加onClick={(e) => {e.stopPropagation(); e.preventDefault();} onClick={(e) => {e.stopPropagation(); e.preventDefault();} on div2. onClick={(e) => {e.stopPropagation(); e.preventDefault();}在 div2 上。

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

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