前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >利用JavaScript获取浏览器计算后的样式

利用JavaScript获取浏览器计算后的样式

作者头像
HTML5学堂
发布2018-03-12 14:34:31
9020
发布2018-03-12 14:34:31
举报
文章被收录于专栏:HTML5学堂HTML5学堂HTML5学堂

HTML5学堂:JavaScript可以用style对象给标签设置样式、获取样式,但是利用style对象获取的样式只能是标签内联的样式,今天要给大家讲解的是利用currentStyle对象与getComputedStyle方法来获取浏览器计算后的样式。

哪些样式是属于浏览器计算后的样式

要检测标签的样式有包含在头部书写样式、标签内联样式和外部的样式,即浏览器计算后的样式。

getComputedStyle(element[, pseudoElt])方法

element用于计算样式的标签;pseudoElt可选指定一个伪元素进行匹配。对于常规的元素来说可省略。

window.getComputedStyle(element[, pseudoElt])方法获取标签在浏览器里计算的样式。

实例

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML5Course - 梦幻雪冰</title>
<link rel="stylesheet" href="reset.css">
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<style>
    .btn {
        width: 100px;
    }
    .btn:after {
        display: block;
        width: 20px;
        height: 20px;
        background-color: red;
        content: ".";
    }
</style>
</head>
<body>
<div class="btn" id="con">HTML5学堂:刘国利、陈能堡</div>
<script>
    var box = document.getElementById("con");
    box.style.height = "1000px";


    // 获取标签浏览器计算后的样式
    console.log(window.getComputedStyle(box, null).getPropertyValue("height"));
    console.log(window.getComputedStyle(box, null).getPropertyValue("width"));
    // 获取伪元素浏览器计算后的样式
    console.log(window.getComputedStyle(box, "after")["content"]);
    console.log(window.getComputedStyle(box, "after")["background-color"]);
    // 注意:getComputedStyle(box, null).getPropertyValue("height")等价于getComputedStyle(box, null)["height"]
</script>
</body>
</html>

关于defaultView

在许多JavaScript框架, getComputedStyle是通过 document.defaultView 对象来调用的。 大部分情况下,这是不需要的,因为可以直接通过window对象调用。但有一种情况,你必需要使用 defaultView, 那是在火狐3.6上访问子框架内的样式 (iframe)——资料来源mozilla

jQuery部分源代码

实例

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML5Course - 梦幻雪冰</title>
<link rel="stylesheet" href="reset.css">
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<style>
    .btn {
        width: 100px;
    }
    .btn:after {
        display: block;
        width: 20px;
        height: 20px;
        background-color: red;
        content: ".";
    }
</style>
</head>
<body>
<div class="btn" id="con">HTML5学堂:刘国利、陈能堡</div>
<script>
    var box = document.getElementById("con");
    box.style.height = "1000px";


    // 获取标签浏览器计算后的样式
    console.log(document.defaultView.getComputedStyle(box, null).getPropertyValue("height"));
    console.log(document.defaultView.getComputedStyle(box, null).getPropertyValue("width"));
    // 获取伪元素浏览器计算后的样式
    console.log(document.defaultView.getComputedStyle(box, "after")["content"]);
    console.log(document.defaultView.getComputedStyle(box, "after")["background-color"]);
</script>
</body>
</html>

浏览器支持程度

currentStyle对象是IE浏览器专有

从上面可以看出IE6~8不支持getComputedStyle该方法,利用currentStyle对象处理兼容咯~

实例

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML5Course - 梦幻雪冰</title>
<link rel="stylesheet" href="reset.css">
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<style>
    .btn {
        width: 100px;
    }
    .btn:after {
        display: block;
        width: 20px;
        height: 20px;
        background-color: red;
        content: ".";
    }
</style>
</head>
<body>
<div class="btn" id="con">HTML5学堂:刘国利、陈能堡</div>
<script>
    var box = document.getElementById("con");
    box.style.height = "1000px";


    // 获取标签浏览器计算后的样式
    console.log(box.currentStyle["width"]);
    console.log(box.currentStyle["height"]);


    // 注意:获取伪元素浏览器计算后的样式——该对象不支持
</script>
</body>
</html>

获取标签浏览器计算后的样式兼容处理

/*
* 功能:获取渲染后标签的样式,element是标签的对象,property是标签样式属性值
* 参数:element是元素对象,property是样式属性
* demo:getStyle(move, "marginLeft");
* author:HTML5学堂
*/
function getStyle(element, property){
var proValue = null;
if (!document.defaultView) {
    proValue = element.currentStyle[property];
} else {
    proValue = document.defaultView.getComputedStyle(element)[property];
}
return proValue;
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2015-11-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 懂点君 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 哪些样式是属于浏览器计算后的样式
  • getComputedStyle(element[, pseudoElt])方法
    • 实例
      • 实例
        • 浏览器支持程度
        • currentStyle对象是IE浏览器专有
          • 实例
          • 获取标签浏览器计算后的样式兼容处理
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档