getComputedStyle与currentStyle获取样式(style/class)

获取通过class 获取样式,obj.currentStyle.样式名          -> IE系列浏览器,getComputedStyle(obj, false)[name] -> chrome FF IE9+

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="智能社 - zhinengshe.com">
<meta name="copyright" content="智能社 - zhinengshe.com">
<title>智能社 - www.zhinengshe.com</title>
<style>
#div1 { 300px; height:200px; background:#ccc; }
</style>
<script>
 //obj.currentStyle.样式名          -> IE系列浏览器
 //getComputedStyle(obj, false)[name] -> chrome FF IE9+

window.onload=function (){
    var oDiv=document.getElementById('div1');
    console.log(getStyle(oDiv, 'width'))
    function getStyle(obj,name){
            return obj.currentStyle ?  obj.currentStyle[name] : getComputedStyle(obj,false)[name];
    }
};
</script>
</head>

<body>
    <div id="div1"></div>
</body>
</html>

参考  http://www.jb51.net/article/34863.htm

原文地址:https://www.cnblogs.com/heboliufengjie/p/4791512.html