如何利用js获取外联style
1<html>
2<head>
3<style>
4.aa{width:100px;height:100px;background:#ccc;}
5</style>
6</head>
7<body>
8<div class="aa" id="aa"></div>
9<input type="button" value="获取" onclick="ddd()">
10<script>
11 var getRuntimeStyle = function(obj, k) {
12 var v = null;
13 if (obj.currentStyle) v = obj.currentStyle[k];
14 else v = window.getComputedStyle(obj, null).getPropertyValue(k);
15 return v;
16 }
17var ddd = function(){
18obj = document.getElementById('aa');
19alert(getRuntimeStyle(obj,'width'));
20alert(getRuntimeStyle(obj,'height'));
21alert(getRuntimeStyle(obj,'backgroundColor'));//IE用 style 的 js 属性,和 FF有区别,FF应该写background-color,即CSS属性
22alert(getRuntimeStyle(obj,'background-color'))
23}
24</script>
25</body>
26</html>
2<head>
3<style>
4.aa{width:100px;height:100px;background:#ccc;}
5</style>
6</head>
7<body>
8<div class="aa" id="aa"></div>
9<input type="button" value="获取" onclick="ddd()">
10<script>
11 var getRuntimeStyle = function(obj, k) {
12 var v = null;
13 if (obj.currentStyle) v = obj.currentStyle[k];
14 else v = window.getComputedStyle(obj, null).getPropertyValue(k);
15 return v;
16 }
17var ddd = function(){
18obj = document.getElementById('aa');
19alert(getRuntimeStyle(obj,'width'));
20alert(getRuntimeStyle(obj,'height'));
21alert(getRuntimeStyle(obj,'backgroundColor'));//IE用 style 的 js 属性,和 FF有区别,FF应该写background-color,即CSS属性
22alert(getRuntimeStyle(obj,'background-color'))
23}
24</script>
25</body>
26</html>