varimgHoverReplaceSrc={init:function(sel){this.sel=sel||"img";this.imgList=document.querySelectorAll(sel);this.work();},work:function(){var_this=this;this.imgList.forEach(function(item){item.addEventListener("mouseenter",function(){_this.changeSrc(item)});item.addEventListener("mouseleave",function(){_this.changeSrc(item)});});},changeSrc:function(item){if(item.getAttribute('data-src')){vartmpSrc=item.src;item.src=item.getAttribute('data-src');item.setAttribute('data-src',tmpSrc);}}}imgHoverReplaceSrc.init("img");
2 回答
潇湘沐
TA贡献1816条经验 获得超6个赞
IE11报错:SCRIPT438:对象不支持“forEach”属性或方法用循环替换forEach是一个可行的解决方案:for(leti=0;i<_this.imgList.length;i++){letimg=_this.imgList[i];img.addEventListener("mouseenter",function(){_this.changeSrc(img);});img.addEventListener("mouseleave",function(){_this.changeSrc(img);})}
添加回答
举报
0/150
提交
取消