结论:this 指的是调用当前方法(函数)的那个对象
1、this应用
按我自己的理解,this如果不在函数中使用指的是window;鼠标点击后调用函数中的this指的是input;鼠标点击调用函数后在调用函数中执行this指的是window
应用一:
需求:鼠标点击按钮后,其背景颜色发生变化
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
</style>
<script type="text/javascript">
window.onload = function (){
var aInput=document.getElementsByTagName("input");
for(var i=0;i<aInput.length;i++){
aInput[i].οnclick=function (){
this.style.backgroundColor="yellow";
alert(this);
}
}
}
</script>
</head>
<body>
<input type="button" value="按钮1" />
<input type="button" value="按钮2" />
<input type="button" value="按钮3" />
</body>
</html>
鼠标点击后调用函数中使用this