<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用闭包来解决的问题:利用函数垫了一层作用域</title>
</head>
<body>
<input type="button" value="按钮1">
<input type="button" value="按钮2">
<input type="button" value="按钮3">
</body>
<script> window.onload=function(){
var aBtn=document.getElementsByTagName('input');
for(var i=0;i<aBtn.length;i++){
(function(i){
aBtn[i].onclick=function(){
alert(i);//0 1 2
};
})(i);
}
}
</script>
</html>
网友评论