以下代码在火狐/ Safari / Chrome 中运行良好,在IE中,“this”似乎失去了handleEvent()函数中的上下文...alert的结果是[object Window],这不是我想要的;当从handleEvent()输出时,“this”需要引用HandleClick对象,而不是Window对象。
我是否遗漏了一些基本的东西导致在IE中出现这种情况?
我是否遗漏了一些基本的东西导致在IE中出现这种情况?
<html>
<head>
<script type="text/javascript">
HandleClick = function(el) {
this.element = document.getElementById(el);
if( this.element.addEventListener ) {
this.element.addEventListener("click", this, false);
} else {
if( this.element.attachEvent ) {
this.element.attachEvent("onclick", this.handleEvent);
}
}
}
HandleClick.prototype = {
handleEvent: function(e) {
alert(this);
}
}
</script>
</head>
<body onload="new HandleClick('logo')"></body>
</html>