this常见错误

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="智能社 - zhinengshe.com" />
<meta name="copyright" content="智能社 - zhinengshe.com" />
<title>智能社 - www.zhinengshe.com</title>

<!--     this:当前事件发生的对象
this常见错误:- window
1 定时器
2 attachEvent 包了一次
3 行间 -->

<script>
function show(){
    alert(this);    
}
window.onload = function(){
    
    
    var aInput = document.getElementsByTagName("input");
    
    aInput[0].onclick = function(){
        show();    // win
    };
    
    aInput[1].onclick = show;//input
    
    aInput[2].onclick = function(){
        setTimeout(show,1000);//win 
    };
    
    aInput[aInput.length - 1].attachEvent("onclick",show);// ie 浏览器 win
    
    
    
};

</script>
</head>

<body>
<input type="button" value="按钮0" />
<input type="button" value="按钮1" />
<input type="button" value="按钮2" />
<input type="button" value="按钮3" onclick="show();"/>
<input type="button" value="按钮4" />

</body>
</html>
原文地址:https://www.cnblogs.com/heboliufengjie/p/4307376.html