JavaScript封装css方法

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>getStyle</title>
<style>
    #div1{width:100px;height:200px;background:yellow;}
</style>
<script>
 
   window.onload = function (){
        var oDiv = document.getElementById('div1');
        function css(obj,name,value){
            if(arguments.length == 2 ){
                if(document.all){
                    return obj.currentStyle[name];
                }else{
                    return getComputedStyle(obj,false)[name];
                }
            }else{
                return obj.style[name] = value;
            }
        }
        css(oDiv,'width','1000px');
    }
</script>
</head>
<body>
    <div id="div1">
        
    </div>
</body>
</html>

你可能感兴趣的:(html)