`

取得css的属性值(兼容)

阅读更多

    如果某一html元素的css是从外部引入的,或者是后来加上去的,用document.getElementById('**').style是取不到正确的值。

 

//elem:元素对象 ; style:css的属性名
function getStyle(elem, style) {
    var value ;
    if (document.all) { //IE
        style = style.replace(/-([a-z])/g, function(s, a){ return a.toUpperCase(); });
        value = elem.currentStyle[style];
    } else {  //FF等
        var css = document.defaultView.getComputedStyle(elem, null);
        value = css ? css.getPropertyValue(style) : null;
    }
    return value == 'auto' ? null : value;
} 



 转自:http://bbs.51js.com/viewthread.php?tid=76359

 以供自己学习一下

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics