js写的鼠标覆盖,更改图片,为什么只在chrome中有效呀,360跟搜狗都没反应?

var imgHoverReplaceSrc = {

        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')) {

                var tmpSrc = item.src;

                item.src = item.getAttribute('data-src');

                item.setAttribute('data-src', tmpSrc);

            }

        }

    }

    imgHoverReplaceSrc.init("img");

<div class="sexangle_1">

                    <img alt="" src="/ehouse/img/item/tourism/yzyj.png"

                        data-src="/ehouse/img/item/tourism/yzyj2.png"> <img alt=""

                        src="/ehouse/img/item/tourism/zjwh.png"

                        data-src="/ehouse/img/item/tourism/zjwh2.png ">

                </div>


慕妹3146593
浏览 549回答 2
2回答

慕码人8056858

IE 11 报错:SCRIPT438: 对象不支持“forEach”属性或方法用循环替换forEach是一个可行的解决方案:for (let i = 0; i < _this.imgList.length; i++) {&nbsp; &nbsp; let img = _this.imgList[i];&nbsp; &nbsp; img.addEventListener("mouseenter", function () {&nbsp; &nbsp; &nbsp; &nbsp; _this.changeSrc(img);&nbsp; &nbsp; });&nbsp; &nbsp; img.addEventListener("mouseleave", function () {&nbsp; &nbsp; &nbsp; &nbsp; _this.changeSrc(img);&nbsp; &nbsp; })}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript