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>
阅读 2.1k
2 个回答

你试试IE浏览器,是不是也是这个问题,若是你就知道是什么情况了

360和搜狗的内核默认和IE内核是一样的,另外打开360的兼容模式,看能否执行事件!

这是网上相关文章的链接:http://blog.csdn.net/dai0941/...

IE 11 报错:

SCRIPT438: 对象不支持“forEach”属性或方法

循环替换forEach是一个可行的解决方案:

for (let i = 0; i < _this.imgList.length; i++) {
    let img = _this.imgList[i];
    img.addEventListener("mouseenter", function () {
        _this.changeSrc(img);
    });
    img.addEventListener("mouseleave", function () {
        _this.changeSrc(img);
    })
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题