您的位置:首页 > Web前端 > CSS

JS使用getComputedStyle()方法获取CSS属性值

2017-06-15 15:18 549 查看
<span style="font-family:Arial;font-size:14px;"><!DOCTYPE html>
<html>
<head>
<title>计算元素样式</title>
<style>
#myDiv {
background-color:blue;
width:100px;
height:200px;
}
</style>
<body>
<div id ="myDiv" style="background-color:red; border:1px solid black"></div>
<script>
var myDiv = document.getElementById("myDiv");
var computedStyle = document.defaultView.getComputedStyle(myDiv, null);
alert(computedStyle.backgroundColor); //"red"
alert(computedStyle.width); //"100px"
alert(computedStyle.height); //"200px"
alert(computedStyle.border); //在某些浏览器中是“1px solid black”
</script>
</body>
</head>
</html></span>
IE中使用的是obj.currentStyle方法,而FF是用的是getComputedStyle 方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: