如果给出此代码,您如何检查文档中单击了哪个项目?
if ( document.addEventListener && document.attachEvent && document.fireEvent ) {
document.attachEvent( "onclick", function() {
// ...
});
}
答案 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);
});
}