简体   繁体   中英

Angularjs access this element in ng-click function

As given in this answer , We need to pass and $event object to the ng-click function, and access the target element using

$scope.setMaster = function(obj, $event){
    console.log($event.target);
}

While event.target isn't a cross-browser property. To overcome this, quirksmode suggests the following

function doSomething(e) {
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
}

Is there any other/better way of getting target element? Like when we bind using DOM/jQuery method, we can use this keyword to refer to clicked element.

Please suggest.

As a library, angularjs does this normalization so there is no need to do the browser specific code our self. it is handled within the library.

The event object passed to the click handler is normalized and will have the said properties irrespective of the browser(given the said browser is supported by the library)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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