简体   繁体   English

如何防止内部 div 的事件处理程序在外​​部 div 的事件处理程序中执行?

[英]How to prevent inner div's event handler from executing in outer div's event handler?

I have two <div> s, one inside the other, each has their own click handler, and i want the outer div's click handler to trigger first when user clicks.我有两个<div> ,一个在另一个里面,每个都有自己的点击处理程序,我希望在用户点击时首先触发外部 div 的点击处理程序。 And inside the outer div's click handler, I want to conditionally check if I want to block the inner div's click handler from executing is that possible?在外部 div 的点击处理程序中,我想有条件地检查是否要阻止内部 div 的点击处理程序执行,这可能吗?

for example in react:例如在反应中:


<div class="outer" onclick={outerHandler}>

<div class="inner onclick={innerHandler}>

Click Me!

<div>

</div>

const outerHandler = () => { console.log("alert outer"); if (something != something) { //block inner handler from executing}}

const innerHandler = () => {console.log("alert inner")};

In the outer handler, handle the event in the capturing phase, and then you can call stopPropagation on the event if you want to prevent it from propagating downward to the inner element:在外部处理程序中,在捕获阶段处理事件,然后如果要防止它向下传播到内部元素,则可以对该事件调用stopPropagation

<div class="outer" onClickCapture={outerHandler}>

(note the use of onClickCapture - this is essential, it indicates that the event is handled in the capturing phase, not the bubbling phase) (注意onClickCapture的使用——这是必不可少的,它表明事件是在捕获阶段处理的,而不是冒泡阶段)

And then, in outerHandler :然后,在outerHandler

const outerHandler = (e) => {
  const stopInnerClick = true; // put your desired logic here
  if (stopInnerClick) {
    e.stopPropagation();
  }
}

Also note that onClick is case-sensitive ( onclick= won't work), and you need a " to terminate the inner class )另请注意, onClick区分大小写( onclick=不起作用),您需要一个"来终止内部class

Live snippet:直播片段:

 const outerHandler = (e) => { const stopInnerClick = true; // put your desired logic here if (stopInnerClick) { console.log('Propagation stopped'); e.stopPropagation(); } }; const innerHandler = () => { console.log('inner'); }; class Comp extends React.Component { render() { return ( <div class="outer" onClickCapture={outerHandler}>outer <div class="inner" onClick={innerHandler}>inner </div> </div> ); } } ReactDOM.render( <Comp />, document.body );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

In React, you could change the state based on whatever condition you want, and then check the value in your handler.在 React 中,您可以根据您想要的任何条件更改状态,然后检查处理程序中的值。

const state = {
    blockInner: false
}

const outerHandler = () => { 
    console.log("alert outer"); 
    if (something !== something) {
        this.setState({ blockInner: true });
    }
}

const innerHandler = () => {
    if (!this.state.blockInner) {
        console.log("alert inner");
    }
}

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

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