IE兼容問題(持續補充)

1、IE下時間格式識別NaN。

原因:IE無法識別yyyy-MM-dd格式,需要轉換爲yyyy/MM/dd。

//正則替換
replace(new RegExp(/-/gm) ,"/")

2、IE10- 無法聲明let const

3、IE8不兼容filter

if (!Array.prototype.filter)
{
    Array.prototype.filter = function(fun /*, thisp */)
    {
        "use strict";

        if (this === void 0 || this === null)
            throw new TypeError();

        var t = Object(this);
        var len = t.length >>> 0;
        if (typeof fun !== "function")
            throw new TypeError();

        var res = [];
        var thisp = arguments[1];
        for (var i = 0; i < len; i++)
        {
            if (i in t)
            {
                var val = t[i]; // in case fun mutates this
                if (fun.call(thisp, val, i, t))
                    res.push(val);
            }
        }

        return res;
    };
}

4、IE10-不兼容JQ的ajax。

// 全局聲明以下代碼
jQuery.support.cors = true;

5、IE若需要兼容@media,需要將css寫入到css文件中,寫在style中無效

6、IE下background-size:cover無效

-ms-filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='dist/images/registerBg.png',sizingMethod=scale); 
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='dist/images/registerBg.png',sizingMethod=scale);

7、事件監聽全兼容

/**
 * 添加事件監聽 兼容IE8
 * @param el
 * @param type
 * @param fn
 */
function addListener(el, type, fn) {
    if (el.addEventListener) {
        el.addEventListener(type, fn, false);
    } else if (el.attachEvent) {
        el.attachEvent('on' + type, fn);
    }
}

/**
 * 移除事件監聽 兼容IE8
 * @param el
 * @param type
 * @param fn
 */
function removeListener(el, type, fn) {
    if (el.removeEventListener) {
        el.removeEventListener(type, fn, false);
    } else if (el.detachEvent) {
        el.detachEvent('on' + type, fn);
    }
}

8、IE8不支持數組的indexOf

if (!Array.prototype.indexOf){
    Array.prototype.indexOf = function(elt /*, from*/){
        var len = this.length >>> 0;

        var from = Number(arguments[1]) || 0;
        from = (from < 0)
            ? Math.ceil(from)
            : Math.floor(from);
        if (from < 0)
            from += len;

        for (; from < len; from++){
            if (from in this && this[from] === elt)
                return from;
        }
        return -1;
    };
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章