简体   繁体   English

在Angular2中获取具有附加事件绑定的元素

[英]Get element with attached event binding in Angular2

I want to get the element with the (click) event binding: 我想使用(click)事件绑定获取元素:

<th class="ui-state-default ui-unselectable-text ui-sortable-column" (click)="reorderComponents($event)">
  <span class="ui-column-title">Component Name</span>
  <span class="ui-sortable-column-icon fa fa-fw fa-sort {{compSortIcon}}"></span>
</th>

As you can see, it is the <th> element. 如您所见,它是<th>元素。 However, after clicking and logging the event object, it shows this: 但是,单击并记录事件对象后,它将显示以下内容:

在此处输入图片说明

在此处输入图片说明

I want to get the <th> element, but sadly I couldn't find a solution yet. 我想获取<th>元素,但遗憾的是我找不到解决方案。

Use event.currentTarget 使用event.currentTarget

reorderComponents(event) {
  console.log(event.currentTarget);
}

It always refers to the element to which the event handler has been attached, as opposed to event.target which identifies the element on which the event occurred. 它始终引用事件处理程序已附加到的元素,而不是event.target ,后者标识发生事件的元素。

Plunker Example 柱塞示例

I hope this help you. 希望对您有所帮助。

reorderComponents(event){
   console.log(event.toElement.nodeName)
}

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

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