以下在firefox/safari/chrome中工作正常,在IE中,"this"似乎在handleEvent()函数中丢失了上下文...警告的结果是[object Window],这不是我想要的; 当从handleEvent()输出时,"this"需要是对HandleClick对象的引用,而不是Window对象.
我错过了一些在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>
Run Code Online (Sandbox Code Playgroud)