简体   繁体   English

如何检测孩子是否点击了父母点击反应

[英]How to detect if child is clicked on parent click React

I'm using React and I have the following situation.我正在使用 React,我有以下情况。

I have one parent div with onClick event, which takes full width and inside that div I have an image.我有一个带有onClick事件的父 div,该事件占用全宽,并且在该 div 内我有一个图像。 I want to be able to know where it is clicked.我希望能够知道它在哪里被点击。 Thus, I want to know if image (child) or div(parent) is clicked.因此,我想知道是否单击了图像(子)或 div(父)。

My code is as follows:我的代码如下:

class APP extends React.Component {
  constructor(props) {
    super(props)   
  }
  
  render() {
    return (
      <div className="parent" onClick={(e) => console.log("PARENT CLICK")}>
        <img src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg"
          style={{maxWidth: "60%", maxHeight: "90%", pointerEvents: 'none', zIndex: 99999}}
          onClick={e => console.log("IMAGE CLICK")}
          />
      </div>
    )
  }
}

ReactDOM.render(<APP />, document.querySelector("#app"))

But it detects always the parent click.但它总是检测到父点击。 I tried to add z-index to the child, but I think that child can't be in front of parent.我试图将z-index添加到孩子,但我认为孩子不能在父母面前。

Here is the fiddle.这是小提琴。

class APP extends React.Component {
 constructor(props) {
 super(props)   
}

handleclick(e){
 e.stopPropagation();
 console.log(e.target.tagName);
 return false;
}   
render() {
  return (
    <div className="parent" onClick={(e) => this.handleclick(e)}>
    <img src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg"
      style={{maxWidth: "30%", maxHeight: "30%", zIndex: 99999}}
      onClick={(e) => this.handleclick(e)}
      />
  </div>
 )
}
} 

ReactDOM.render(<APP />, document.querySelector("#app"))

please note here I added e.stopPropagation() in the click event which only executes with the target element.. here you can read more about propogation请注意,我在单击事件中添加了e.stopPropagation() ,该事件仅与目标元素一起执行。在这里您可以阅读有关传播的更多信息

and also please remove the CSS pointerEvents: 'none' from the img tag, it works fine.并且请从img标签中删除 CSS pointerEvents: 'none' ,它工作正常。

Working Fiddle工作小提琴

You have pointerEvents set to none in your img 's inline style object.您在img的内联style object 中将pointerEvents设置为none Remove that.删除它。 Can remove zIndex as well.也可以删除zIndex

From CSS-Tricks :-来自CSS 技巧:-

pointer-events:none prevents all click, state and cursor options on the specified HTML element pointer-events:none阻止所有点击,state 和 cursor 选项在指定的 HTML 元素上

You don't need e.stopPropagation() here.您在这里不需要e.stopPropagation() Just set the event handler only on parent like so (this is known as event delegation ):-只需像这样仅在级上设置事件处理程序(这称为事件委托):-

class APP extends React.Component {
  constructor(props) {
    super(props)   
  }
  
  handleclick(e){
    console.log(e.target.tagName);
  }  
  render() {
    return (
      <div className="parent" onClick={this.handleclick}>
        <img src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg"
          style={{maxWidth: "30%", maxHeight: "30%"}}
          />
      </div>
    )
  }
}

ReactDOM.render(<APP />, document.querySelector("#app"))

pointerEvents: none will block the pointer events.Remove that from your styling pointerEvents: none 将阻止指针事件。从您的样式中删除它

Im not sure if this is the react way of doing this but in javascript you can use the event object properties to get the element that triggered it with event.target and the element that handled it with event.currentTarget .我不确定这是否是这样做的反应方式,但在 javascript 中,您可以使用事件 object 属性来获取使用event.target触发它的元素以及使用event.currentTarget处理它的元素。

 document.querySelector('#parent').addEventListener('click', (e) => { console.log(`clicked element is: ${e.target.id}`); console.log(`event handler element is: ${e.currentTarget.id}`); });
 <div id='parent'>parent <div id='child'>child</div> </div>

As CodeBug noted above it should be enough to stop the propagation on the click event by calling (inside of the onClick function):正如上面提到的CodeBug ,它应该足以通过调用(在onClick函数内部)停止点击事件的传播

event.stopPropagation();

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

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