简体   繁体   English

如何获取在组件内部单击的子元素?

[英]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.我有一个组件正在加载在 html 中加载的第三方脚本。

I am trying to detect when a specific part of the injected html elemen is clicked.我试图检测何时单击了注入的 html 元素的特定部分。

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.我得到的东西基本上是 Container className 但我想要点击元素的来源。

How do I do this?我该怎么做呢?

event.currentTarget is the target of the attached event. event.currentTarget是附加事件的目标。 Use event.target to get where the event started.使用event.target获取事件开始的位置。

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

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

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