部分js兼容性

js的兼容

1、屬性href,在IE6、7下則返回的是全路徑,修正方法是設置getAttribute第二個參數爲2(期望嚴格按照文檔設置的屬性值返回,只有IE會識別第二個參數);
2、width和height不是行內的樣式,不可以使用ele.getAttribute(“”)獲取,應使用ele.offsetWidth獲取,但是offsetWidth獲取到的值包含padding+width;
3、select默認option的selected是否爲true,當在html文檔直接寫的時候第一個selected爲true,但是在IE下通過js插入一段select,則第一默認option的selected是false,解決方法:執行parent.selectedIndex,之後就OK;
4、<div style="top: 1px;"></div>行內樣式style獲取ele.style,但是在IE6、7支持ele.style.cssText則返回style行內樣式的值;
5、setgetAtrribute在IE6、7獲取內聯屬性時,不支持html屬性支持DOM屬性,比如ele.getAttribute(“class”),在IE6、7無法獲取到,需要的是className。修正方法:

var fixSpecified = {
        name: true,
        id: true
    };
function get(ele, name) {
    var ret = ele.getAttributeNode(name);
    return ret && (fixSpecified[name] ? ret.nodeValue = "" : ret.specified) ? ret.nodeValue : undefined;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章