[英]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.