最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - react: onClick - get only the clicked element, not the children - Stack Overflow

programmeradmin2浏览0评论

I have a button, inside this button is a fontawesome svg. The listener is on the button only, not on the svg. Though, when I click on the svg, the event.target is not the button.

But I only need the button as target.

Here's the code:

const icon = '<svg aria-hidden="true" data-prefix="fas" data-icon="paragraph" class="svg-inline--fa fa-paragraph fa-w-14 " role="img" xmlns="" viewBox="0 0 448 512"><path fill="currentColor" d="M408 32H177.531C88.948 32 16.045 103.335 16 191.918 15.956 280.321 87.607 352 176 352v104c0 13.255 10.745 24 24 24h32c13.255 0 24-10.745 24-24V112h32v344c0 13.255 10.745 24 24 24h32c13.255 0 24-10.745 24-24V112h40c13.255 0 24-10.745 24-24V56c0-13.255-10.745-24-24-24z"></path></svg>';

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

  handleClick(event) {
    event.stopPropagation();
    console.log(event.target);
  }

  render() {
    return (
      <div>
        <button onClick={this.handleClick}>
          <span dangerouslySetInnerHTML={{__html: icon}} />
        </button>
      </div>
    )
  }
}

ReactDOM.render(<App />, document.getElementById('root'));

I wrote a little pen:

I have a button, inside this button is a fontawesome svg. The listener is on the button only, not on the svg. Though, when I click on the svg, the event.target is not the button.

But I only need the button as target.

Here's the code:

const icon = '<svg aria-hidden="true" data-prefix="fas" data-icon="paragraph" class="svg-inline--fa fa-paragraph fa-w-14 " role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path fill="currentColor" d="M408 32H177.531C88.948 32 16.045 103.335 16 191.918 15.956 280.321 87.607 352 176 352v104c0 13.255 10.745 24 24 24h32c13.255 0 24-10.745 24-24V112h32v344c0 13.255 10.745 24 24 24h32c13.255 0 24-10.745 24-24V112h40c13.255 0 24-10.745 24-24V56c0-13.255-10.745-24-24-24z"></path></svg>';

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

  handleClick(event) {
    event.stopPropagation();
    console.log(event.target);
  }

  render() {
    return (
      <div>
        <button onClick={this.handleClick}>
          <span dangerouslySetInnerHTML={{__html: icon}} />
        </button>
      </div>
    )
  }
}

ReactDOM.render(<App />, document.getElementById('root'));

I wrote a little pen: https://codepen.io/DaFunkyAlex/pen/aRvVjd

Share Improve this question asked Oct 2, 2018 at 9:48 DaFunkyAlexDaFunkyAlex 1,9692 gold badges25 silver badges36 bronze badges 4
  • Posible duplicate of this question: stackoverflow.com/questions/29168719/… – Mirosław Commented Oct 2, 2018 at 9:56
  • Yes it is... sry – DaFunkyAlex Commented Oct 2, 2018 at 10:01
  • Possible duplicate of Can you target an elements parent element using event.target? – Stephen Kennedy Commented Oct 2, 2018 at 10:46
  • You can check also with ease if e.target === e.currentTarget . The e.target tells your the actual clicked element where e.currentTarget is the parent element with the click listener. – konsalex Commented Apr 14, 2020 at 15:25
Add a comment  | 

2 Answers 2

Reset to default 22

You should use: event.currentTarget, which is always the object listening for the event.

The event.target is the actual target that received the click.

It can be done in multiple ways. I prefer the CSS way of doing it, applying pointer-events : none to the children span.

Codepen : https://codepen.io/imsontosh/pen/mzeqNx

button { 
  background: #eee;
  border: 1px solid #ddd; 
  width: 150px;
  span{
    pointer-events:none;
  }
}
发布评论

评论列表(0)

  1. 暂无评论