方法一:给每个按钮 自定义data-index属性
for(var i = 0; i < btns.length; i++){btns[i].setAttribute('data-index',i)btns[i].onclick = function(){console.log(this.dataset['index'])}}
方法二:存下标
for(var i = 0; i < btns.length; i++){btns[i].index = i;btns[i].onclick = function(){console.log(this.index)}}
方法三:forEach
btns.forEach(function(item, index) {item.onclick = function() {console.log(index)}})
方法四let
for(let i = 0; i< btns.length; i++){btns[i].onclick = function(){console.log(i)}}
方法五:闭包
for(var i = 0; i < btns.length; i++){(function(i){btns[i].onclick = function(){console.log(i)} })(i)}
方法六:闭包
for(var i = 0; i < btns.length; i++){btns[i].onclick = (function(i){return function(){console.log(i)}})(i)}