<button>按钮一</button>
<button>按钮二</button>
<button>按钮三</button>
<button>按钮四</button>
<script type="text/javascript">
var btn = document.querySelectorAll("button");
for(var i = 0;i<btn.length;i++){
btn[i].onclick = function(){
alert(i);
}
}
<button>按钮一</button>
<button>按钮二</button>
<button>按钮三</button>
<button>按钮四</button>
<script type="text/javascript">
var btn = document.querySelectorAll("button")
//闭包函数
function fun(index){
return function() {
alert(index);
}
}
for(var i = 0;i<btn.length;i++){
btn[i].onclick = fun(i);
}
</script>