您好,欢迎访问一九零五行业门户网

如何通过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>  <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属性值 ,更多相关内容请关注。
相关推荐:
如何通过js来判断碰撞方法
如何使用css实现圈人效果(css sprites)
如何通过php 进行aes256加密算法
以上就是如何通过js获取css属性值的详细内容。
其它类似信息

推荐信息