简体   繁体   中英

How to get the element clicked on?

How would you check to see which item was clicked in the document, given this code?

if ( document.addEventListener && document.attachEvent && document.fireEvent ) {
    document.attachEvent( "onclick", function() {
     // ...
    });
}
// using e.srcElement or event.srcElement

try this :) and see this http://www.quirksmode.org/js/events_advanced.html

if (document.addEventListener){
    document.addEventListener("click", function(event){
        var targetElement = event.target || event.srcElement;
        console.log(targetElement);
    });
} else if (document.attachEvent) {    
    document.attachEvent("onclick", function(){
        var targetElement = event.target || event.srcElement;
        console.log(targetElement);
    });
}

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