1. 程式人生 > 實用技巧 >jquery報錯:jquery.tools.min.js:15 Uncaught TypeError: Cannot read property 'msie' of undefined

jquery報錯:jquery.tools.min.js:15 Uncaught TypeError: Cannot read property 'msie' of undefined

jquery報錯:jquery.tools.min.js:15 Uncaught TypeError: Cannot read property 'msie' of undefined

背景:

首頁用到了jquery.tools.min.js這個外掛,用於輪播圖的顯示,但是突然就不行了,誰也沒動,之前也沒有出現過,檢視控制檯,報錯顯示:jquery.tools.min.js:15 Uncaught TypeError: Cannot read property 'msie' of undefined

解決

查閱部落格發現,這是由於不支援之前的jquery才出錯的。到現在解決方法有兩種:

一、將程式碼貼上進去原始碼儲存即可

jQuery.browser = {};
(function () {
    jQuery.browser.msie = false;
    jQuery.browser.version = 0;
    if (navigator.userAgent.match(/MSIE ([0-9]+)./)) {
        jQuery.browser.msie = true;
        jQuery.browser.version = RegExp.$1;
    }
})();

至於貼上的位置:

先找到jquery.tools.min.js這個檔案,格式化程式碼,然後ctrl+f查詢msie:

a.tools = a.tools || {version: "v1.2.7"}, a.tools.overlay = {
    addEffect: function (a, b, d) {
        c[a] = [b, d]
    },
    conf: {
        close: null,
        closeOnClick: !0,
        closeOnEsc: !0,
        closeSpeed: "fast",
        effect: "default",
        fixed: !a.browser.msie || a.browser.version > 6,   //重點在這行
        left: "center",
        load: !1,
        mask: null,
        oneInstance: !0,
        speed: "normal",
        target: null,
        top: "10%"
    }
};

然後在這個方法的上面新增這段程式碼就行:

jQuery.browser = {};
(function () {
    jQuery.browser.msie = false;
    jQuery.browser.version = 0;
    if (navigator.userAgent.match(/MSIE ([0-9]+)./)) {
        jQuery.browser.msie = true;
        jQuery.browser.version = RegExp.$1;
    }
})();

所以完整的程式碼:

(function (a) {
    jQuery.browser = {};
    (function () {
        jQuery.browser.msie = false;
        jQuery.browser.version = 0;
        if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) {
            jQuery.browser.msie = true;
            jQuery.browser.version = RegExp.$1;
        }
    })();
    a.tools = a.tools || {version: "v1.2.7"}, a.tools.overlay = {
        addEffect: function (a, b, d) {
            c[a] = [b, d]
        },
        conf: {
            close: null,
            closeOnClick: !0,
            closeOnEsc: !0,
            closeSpeed: "fast",
            effect: "default",
            fixed: !a.browser.msie || a.browser.version > 6,
            left: "center",
            load: !1,
            mask: null,
            oneInstance: !0,
            speed: "normal",
            target: null,
            top: "10%"
        }
    };
    var b = [], c = {};
    a.tools.overlay.addEffect("default", function (b, c) {
        var d = this.getConf(), e = a(window);
        d.fixed || (b.top += e.scrollTop(), b.left += e.scrollLeft()), b.position = d.fixed ? "fixed" : "absolute", this.getOverlay().css(b).fadeIn(d.speed, c)
    }, function (a) {
        this.getOverlay().fadeOut(this.getConf().closeSpeed, a)
    });
    ......

二、使用外掛jQuery Migrate來修復bug

下載連結的話,可以百度。

結論

你的先確定你是jquery.tools.min.js這個外掛所造成的的問題。博主使用的是第一種方法,可以解決問題。第二種沒試過。至於為什麼要這樣改,請看參考資料。

  1. [jQuery] Cannot read property ‘msie’ of undefined錯誤的解決方法
  2. [JQUERY] CANNOT READ PROPERTY ‘MSIE’ OF UNDEFINED錯誤的解決方法