取非行间样式

html代码:

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

css代码:

<style type="text/css">
            #div1{width:100px;height: 100px;}
</style>

js代码:

<script type="text/javascript">
            window.onload=function(){
                function getStyle(obj,name){
                    if(obj.currentStyle){
                        return obj.currentStyle[name];
                    }else{
                        return getComputedStyle(obj,false)[name];
                    }
                }
                var oDiv=document.getElementById("div1");
                alert(getStyle(oDiv,"width"));
            }
</script>

取行间样式的js代码:

window.onload=function(){
                function css(obj,name,value){
                    if(arguments.length==2){   //两个参数的时候是获取样式
                        return obj.style[name];
                    }else{     //三个参数
                        obj.style[name]=value;
                    }
                }
                var oDiv=document.getElementById("div1");
                alert(css(oDiv,"width"));
}
原文地址:https://www.cnblogs.com/rain92/p/5681484.html