Angular2 get clicked element id

前端 未结 9 1930
深忆病人
深忆病人 2020-11-29 20:07

I have such click event

 
 
9条回答
  •  鱼传尺愫
    2020-11-29 20:52

    If you want to have access to the id attribute of the button you can leverage the srcElement property of the event:

    import {Component} from 'angular2/core';
    
    @Component({
      selector: 'my-app',
      template: `
        
      `
    })
    export class AppComponent {
      onClick(event) {
        var target = event.target || event.srcElement || event.currentTarget;
        var idAttr = target.attributes.id;
        var value = idAttr.nodeValue;
      }
    }
    

    See this plunkr: https://plnkr.co/edit/QGdou4?p=preview.

    See this question:

    • How can I make event.srcElement work in Firefox and what does it mean?

提交回复
热议问题