IE currentStyle && getComputedStyle

用document.getElementById(‘element').style.xxx可以获取元素的样式信息,

可是它获取的只是DOM元素style属性里的样式规则,对于通过class属性引用的外部样式表就获取不到了


IE可以识别 currentStyle ,其他浏览器可以识别 getComputedStyle :

function getStyle(obj, attr) 

if(obj.currentStyle) 

return obj.currentStyle[attr]; 

else 

return getComputedStyle(obj,null)[attr]; 



/*
document.defaultView.getComputedStyle (jQuery)

也可以先 获取样式,然后,IE浏览器 :getAttribute其他 getPropertyValue获取属性值
oStyle = obj.currentStyle? obj.currentStyle : window.getComputedStyle(obj, null);
if (oStyle.getPropertyValue) {
            alert("getPropertyValue下背景色:" + oStyle.getPropertyValue("background-color"));
        } else {
            alert("getAttribute下背景色:" + oStyle.getAttribute("backgroundColor"));
        }
*/

 

posted @ 2017-10-24 14:04  Esther_Cheung  阅读(200)  评论(0编辑  收藏  举报