取制定元素的最终样式

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>test</title>
<link rel="stylesheet" type="text/css" href="css.css">
</head>
<body>
    <div class="head">我的天空</div>
    <div>
    <table class="table">
        <tr>
            <td>
                我的天空
            </td>
            <td>
                我的天空
            </td>
            <td>
                我的天空
            </td>
            <td>
                我的天空
            </td>
            <td>
                我的天空
            </td>
            <td>
                我的天空
            </td>
        </tr>
    </table>
</div>
    
</body>
</html>

<script>

function Toword(obj,arr){
    this.obj=obj;
    this.arr=arr;
    this.init(this.obj,this.arr);
}

Toword.prototype.getcss=function(obj,attr){
    return obj.currentStyle||getComputedStyle(obj,false)[attr];
}
Toword.prototype.init=function(){
    if(this.obj.length){
        for (var j = 0; j < this.obj.length; j++) {
            for (var i = 0; i < this.arr.length; i++) {
                this.obj[j].style[this.arr[i]]=this.getcss(this.obj[j],this.arr[i]);
            }
        }
    }else{
            for (var i = 0; i < this.arr.length; i++) {
                this.obj.style[this.arr[i]]=this.getcss(this.obj,this.arr[i]);
            }
    }
}

var oDiv=document.querySelector(".head");
var aTd=document.querySelectorAll("td");
var oTable=document.querySelector("table");
new Toword(oTable,["width","borderCollapse"])
new Toword(oDiv,["fontSize","backgroundColor","borderLeftWidth","borderLeftColor","borderLeftStyle","borderRightWidth","borderRightColor","borderRightStyle","borderBottomWidth","borderBottomColor","borderBottomStyle","borderTopWidth","borderTopColor","borderTopStyle"])
new Toword(aTd,["fontSize","borderLeftWidth","borderLeftColor","borderLeftStyle","borderRightWidth","borderRightColor","borderRightStyle","borderBottomWidth","borderBottomColor","borderBottomStyle","borderTopWidth","borderTopColor","borderTopStyle","lineHeight","paddingLeft","paddingRight","paddingBottom","paddingTop"])

    
</script>
原文地址:https://www.cnblogs.com/busicu/p/4274011.html