简体   繁体   English

如何获取event.target以返回特定的target元素?

[英]how do I get event.target to return a specific target element?

In my react application I have this button which acts like state switcher this button has an id attribute/prop and this button wraps a font-awesome icon. 在我的React应用程序中,我有一个按钮,其作用类似于状态切换器,该按钮具有id属性/ prop,并且该按钮包装了真棒字体图标。

<button id={feedId}>
   <i className={isStatus === 'Y' ? 'fa fa-unlock' : 'fa fa-lock'} /> 
   {isStatus === 'Y' ? 'Active' : 'Inactive'}
</button>

Now depending upon where you click on the button the event.target would return the entire button with id or just the icon that's <i> tag how do I make sure that entire button gets return as part of event.target instead of just <i> 现在,根据您在按钮上单击的位置, event.target将返回带有id的整个按钮或仅是<i>标记的图标。 <i>如何确保整个按钮作为event.target的一部分返回,而不仅仅是<i>

You can use currentTarget 您可以使用currentTarget

 function test(e) { console.log('Target ', e.target); console.log('Current Target ', e.currentTarget) } 
 <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" /> <button id="test" onclick="test(event)"> <i class="fa fa-quora" aria-hidden="true"></i> Text </button> 

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

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