-
Notifications
You must be signed in to change notification settings - Fork 6
/
015-获取div样式.html
64 lines (61 loc) · 1.15 KB
/
015-获取div样式.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style>
*{ margin:0px; padding:0px;list-style:none; font-size:12px;}
#div1{ width:200px; height:200px; background:#FF0004;}
</style>
<script>
/*
重点
arguments 类数组对象
currentStyle 获取样式,支持ie浏览器
getComputedStyle 获取样式,支持火狐浏览器
*/
function getStyle(obj, arr)
{
if(obj.currentStyle)//如果是ie浏览为真
{
return obj.currentStyle[arr];
}
else
{
return getComputedStyle(obj,false)[arr];
}
}
function getCss(obj, arr, value)
{
if(arguments.length == 2)
{
return getStyle(obj,arr);
}
if(arguments.length == 3)
{
obj.style[arr] = value;
}
}
window.onload = function ()
{
var OBtn1 = document.getElementById("btn1");
var OBtn2 = document.getElementById("btn2");
var ODiv1 = document.getElementById("div1");
OBtn1.onclick = function()
{
alert(getCss(ODiv1,'width'));
}
OBtn2.onclick = function()
{
getCss(ODiv1,'background','green');
}
}
</script>
</head>
<body>
<input id="btn1" type="button" value="获取宽度" />
<input id="btn2" type="button" value="改变颜色" />
<div id="div1"></div>
</div>
</body>
</html>