<!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属性值 ,更多相关内容请关注php中文网。
相关推荐:
以上就是如何通过JS获取CSS属性值的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号