JavaScript 获取当前样式

<div id="box" class="box">
    sss
</div>

使用外部样式文件 1.css:

*{
    margin: 0;
    padding: 0;
}


.box{
    width: 320px;
    background: #f00;
    transform:translateX(30px);
}

JS 获取当前样式方法:

let getStyle = function(tag,prop){
    return tag.currentStyle? tag.currentStyle[prop] : getComputedStyle(tag)[prop] ;
};

let box = document.getElementById("box");
console.info( getStyle(box,"width"));   // 320px

猜你喜欢

转载自blog.csdn.net/weixin_42703239/article/details/108128222