如何利用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
}
17
var ddd = function(){
18
obj = document.getElementById('aa');
19
alert(getRuntimeStyle(obj,'width'));
20
alert(getRuntimeStyle(obj,'height'));
21
alert(getRuntimeStyle(obj,'backgroundColor'));//IE用 style 的 js 属性,和 FF有区别,FF应该写background-color,即CSS属性
22
alert(getRuntimeStyle(obj,'background-color'))
23
}
24
</script>
25
</body>
26
</html>
<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
}17
var ddd = function(){18
obj = document.getElementById('aa');19
alert(getRuntimeStyle(obj,'width'));20
alert(getRuntimeStyle(obj,'height'));21
alert(getRuntimeStyle(obj,'backgroundColor'));//IE用 style 的 js 属性,和 FF有区别,FF应该写background-color,即CSS属性22
alert(getRuntimeStyle(obj,'background-color'))23
}24
</script>25
</body>26
</html>


.aa
浙公网安备 33010602011771号