原生JavaScript获取css样式

访问属性:obj.attr 或者 obj['attr']

通过js访问style属性 :

document.getElementById("main").style.backgroundColor; 

style 只能获取元素的内联样式。因此,要获取元素的完整的样式信息,必须使用 window 对象的 getComputedStyle 方法,此方法有2个参数,第一个参数为要获取计算样式的元素,第二个参数可以是null、空字符串、伪类(如:before,:after),这两个参数 都是必需的。

完整实例:

<!doctype html>
<html>
<head>
    <style>
      
        #mask {
            position: absolute;
            z-index: 1000;
            top: 10px; left: 0;
            background: blue;
            width: 100px;
            height: 100px;
        }
    </style>
</head>
<body>
 
<div  ></div>
<script>
    var mask = document.getElementById('mask');
    var style = window.getComputedStyle(mask, "");
    console.log('34',style.position,mask.style.border);

</script>
</body>
</html>