zoukankan      html  css  js  c++  java
  • JS获取CSS属性值

    <!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="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>
  • 相关阅读:
    数据库子句
    数据查询的语言
    试图
    Bootstrap 简介及引用方法
    一阶段项目整理
    js 鼠标移入移出
    js 鼠标点击事件
    轮播图
    滚动条 固定导航栏
    三元运算符 DOM找元素
  • 原文地址:https://www.cnblogs.com/houkaihua/p/4773291.html
Copyright © 2011-2022 走看看