简体   繁体   中英

How to get child element that was clicked inside of a component?

I have a component that is loading a third part script which loads in html.

I am trying to detect when a specific part of the injected html elemen is clicked.

basically:

<div>
<a></a> // detect that this linked was clicked. 
</div>

What I have:

<Container onClick={(event) => {
                    event.stopPropagation();
                    const element = event.currentTarget as HTMLElement;
                    console.log('EVENT IS', element.ClassName)
}>
 // Injected thirdparty html
</Container>

The thing I am getting is basically the Container className butI want the source of the clicked element.

How do I do this?

event.currentTarget is the target of the attached event. Use event.target to get where the event started.

https://developer.mozilla.org/en-US/docs/Web/API/Event/currentTarget

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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