通过getPropertyValue 取得最终的CSS样式

来源:互联网 发布:js动态为div添加style 编辑:程序博客网 时间:2024/05/23 01:10

新学习一个js 的方法 getPropertyValue   (实现 js框架中  css 的最终调用的函数),取得元素最终计算出的css 样式



DEMO:

<!DOCTYPE html>

<html>
<head>
    <title></title>
    <meta charset="gbk">
    <style type="text/css">
        #content {
            color: red !important;;
        }
    </style>
</head>
<body>
<div id="content" style="color:yellow">content</div>
</body>


<script type="text/javascript">
    var a = document.getElementById("content");
    alert("style "+a.style.color);
    alert("css "+window.getComputedStyle(a).getPropertyValue("color"));
</script>
</html>
0 0