鍍金池/ 問答/HTML/ js寫的鼠標覆蓋,更改圖片,為什么只在chrome中有效呀,360跟搜狗都沒反應(yīng)

js寫的鼠標覆蓋,更改圖片,為什么只在chrome中有效呀,360跟搜狗都沒反應(yīng)?

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>
回答
編輯回答
小眼睛

IE 11 報錯:

SCRIPT438: 對象不支持“forEach”屬性或方法

循環(huán)替換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);
    })
}
2018年8月26日 04:54
編輯回答
莓森

你試試IE瀏覽器,是不是也是這個問題,若是你就知道是什么情況了

360和搜狗的內(nèi)核默認和IE內(nèi)核是一樣的,另外打開360的兼容模式,看能否執(zhí)行事件!

這是網(wǎng)上相關(guān)文章的鏈接:http://blog.csdn.net/dai0941/...

2017年12月27日 06:54