最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular click event on component send inner element as target - Stack Overflow

programmeradmin1浏览0评论

I have a click event on an Angular ponent:

import { Component } from "@angular/core";

@Component({
  template: `
    <hello (click)="onClick($event)"></hello>
  `
})
export class AppComponent {
  onClick(e) {
    alert("Click on TAG: " + e.target.tagName);
  }
}

See demo online: ponent.ts

Why is event.target an inner element of the ponent and not the ponent itself?

I have a click event on an Angular ponent:

import { Component } from "@angular/core";

@Component({
  template: `
    <hello (click)="onClick($event)"></hello>
  `
})
export class AppComponent {
  onClick(e) {
    alert("Click on TAG: " + e.target.tagName);
  }
}

See demo online: https://stackblitz./edit/angular-ivy-zxmsnc?file=src%2Fapp%2Fapp.ponent.ts

Why is event.target an inner element of the ponent and not the ponent itself?

Share Improve this question edited Apr 13, 2021 at 10:03 Tim 5,8898 gold badges50 silver badges67 bronze badges asked Apr 13, 2021 at 10:02 ar099968ar099968 7,59715 gold badges73 silver badges139 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

That's because event.target refers to the element that triggered the event.

I think you're looking for event.currentTarget which refers to the element that the event listener is attached to.

发布评论

评论列表(0)

  1. 暂无评论