var render={
addEvent: function (elm, evType, fn) {
if (elm.addEventListener) {
elm.addEventListener(evType, fn,false);//DOM2.0
return;
}
else if (elm.attachEvent) {
var r = elm.attachEvent('on' + evType, fn);//IE5+
return;
}
},
init:function () {
var _this=this;
var selectBtn= document.getElementById('selectItem').getElementsByTagName('p');
for(var i=0,j=selectBtn.length;i
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
_this.addEvent(selectBtn[i],'click',function () { 回调 函数是异步的,你上面的for 也许早就跑完了 ,导致 回调函数中的i =2
你只是写了个立即执行函数,并没有进行传参
你可以类似这个来写,
还有可以就是,不用闭包,在你的循环里面不要用 var 定义 i ,用 let 定义 i , ES6 的特性。