简体   繁体   English

Angular2获取触发事件的元素

[英]Angular2 Get element where an event is fired from

Here is my code : 这是我的代码:

<div id="foo" (click)="$event.target.classList.toggle('selected')">
   <div>...</div>
   <div>...</div>
   <div>...</div>
</div>

I would like to toggle the class 'selected' on the div#foo thus $event.target isn't correct since clicking one of the inside divs will return that div and not div#foo . 我想在div#foo上切换“选定”类,因此$event.target不正确,因为单击内部div之一将返回该div而不是div#foo Any idea how to always target div#foo when clicking on one of the element inside ? 任何想法如何在单击其中一个元素时始终以div#foo为目标?

use the currentTarget instead of target 使用currentTarget而不是target

<div id="foo" (click)="$event.currentTarget.classList.toggle('selected')">
   <div>...</div>
   <div>...</div>
   <div>...</div>
</div>

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

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