首页 > Web开发 > 详细

JS获取CSS属性值

时间:2015-08-31 17:16:21      阅读:237      评论:0      收藏:0      [点我收藏+]
<!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>
  <div id="f" style="width:200px; height:200px;"></div>

  <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属性值

原文:http://www.cnblogs.com/houkaihua/p/4773291.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!