JS:如何点击元素?

时间:2012-07-31 13:20:45

标签: javascript jquery

如果给出此代码,您如何检查文档中单击了哪个项目?

if ( document.addEventListener && document.attachEvent && document.fireEvent ) {
    document.attachEvent( "onclick", function() {
     // ...
    });
}

1 个答案:

答案 0 :(得分:30)

// using e.srcElement or event.srcElement

试试这个:) 并看到这个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);
    });
}