

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title> JS获取CSS属性 </title>
<style type="text/css">
#f{background-color:#FF0000;}
</style>
</head>
<body>
<p id="f" style="width:200px; height:200px;"></p>
<script type="text/javascript">
var o = document.getElementById('f');
document.write(o.style.width + '<br>'); // 200px
document.write(o.style.backgroundColor + '<br>'); // 空白
function getDefaultStyle(obj,attribute){
return obj.currentStyle?obj.currentStyle[attribute] : document.defaultView.getComputedStyle(obj,false)[attribute];
}
document.write(getDefaultStyle(o, 'width') + '<br>'); // 200px
document.write(getDefaultStyle(o, 'backgroundColor')); // #FF0000
</script>
</body>
</html>
JS獲取CSS屬性方法:
function getDefaultStyle(obj,attribute){
return obj.currentStyle?obj.currentStyle[attribute] : document.defaultView.getComputedStyle(obj,false)[attribute];
}本文讲解了如何通过JS获取CSS属性值 ,更多相关内容请关注Gxl网。
相关推荐:
如何通过JS来判断碰撞方法
如何使用CSS实现圈人效果(CSS Sprites)
如何通过PHP 进行AES256加密算法
