yulei's blog

有梦想更有要有行动,每天前进一小步,那么每年可以迈出一大步 QQ:65072096 MSN:coolsoft2001@sina.com

导航

转载 JS获取CSS属性值

obj.style方法,这个方法只能JS只能获取写在html标签中的写在style属性中的值(style="..."),看一下代码
[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  % O( w  K5 ^$ s# q  r6 b  G( v/ l
<title>JS获取CSS属性值</title>  " o% E9 b" e4 B. _; a9 P! V/ H
<style type="text/css">  ( ?9 E0 j7 x8 u8 _
<!--  . }" h0 ^+ s; z- P$ `
.ss{color:#cdcdcd;}  : I& [5 R; ^3 _6 v
-->  
</style>  - N2 L4 P; I2 T9 j3 R
</head>  . q: {. l; M) G1 J! F, }, I" z
<body>  : O% "# ?5 Q; u; g) P8 i
<div id="css88" class="ss" style="width:200px; height:200px; background:#333333">JS获取CSS属性值</div>  - U8 w+ o5 t! Z# A: {3 f
<script type="text/javascript">  7 Y( [/ q: A) |1 A
    alert(document.getElementById("css88").style.width);//200px   ! q+ s7 B& G4 I8 A
    alert(document.getElementById("css88").style.color);//空白   
</script>      ' N7 N: K: q8 v2 K; {+ E
</body>  
</html> 
[/code]
上面这个例子对id为"css88"的div设置了2种烦事的样式,包括style和外部样式class。
9 q0 A+ ~  U# S+ q$ X1 R
从alert出来的信息可以看到,document.getElementById("css88").style方法获取不到class为ss的属性和值。
, u- G( F) Q3 m2 C+ b
那么这么样才能获取到class为ss的属性和值呢?
! C2 P* l! E0 }% c3 k
IE中使用的是obj.currentStyle方法,而FF是用的是getComputedStyle 方法。4 b- L. ^/ @# b0 w- y5 X

网上一个比较 方法是:
[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  1 g/ m3 x) g! T6 q
<head>  ( e$ |! N% _( s1 m
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    x' q5 V8 K6 ~" ^
<title>S获取CSS属性值</title>  6 n4 x' ]( b) ]- w1 K6 E* s
  
<style type="text/css">  1 X/ X" U+ K# T4 c5 q; G- w# F4 w
<!--  
.ss{background:blue; color:#cdcdcd; width:200px}  
-->  
</style>  
</head>  6 h2 c9 o1 d! U7 ^' t
  ( I& U" }0 J3 Q5 P7 Q
<body>  1 `8 X  l' j8 l4 y; v* y
<p id="qq" class="ss" >sdf</p>  7 U' v$ [  D- ?1 U0 S5 ?
  $ |  w: u2 x6 V3 {( Z, G
<script type="text/javascript">  
function GetCurrentStyle (obj, prop) {     
    if (obj.currentStyle) {        2 d, j% j* T3 R& Z1 M) A/ g/ N
        return obj.currentStyle[prop];     
    }      3 @$ e0 u% I/ f
    else if (window.getComputedStyle) {        + x# s, X+ Z7 "2 "
        propprop = prop.replace (/([A-Z])/g, "-$1");           
        propprop = prop.toLowerCase ();        
         return document.defaultView.getComputedStyle (obj,null)[prop];     
    }      
    return null;   8 N0 o3 "3 K1 a2 u  f) k3 g7 m, u
}   ; P3 @4 L$ d+ m3 o' g9 S* {
var dd=document.getElementById("qq");   
alert(GetCurrentStyle(dd,"width"));   ! h6 z' G7 A/ T
</script>  $ D& c# Z& Z1 p- ]5 K/ v
</body>  
</html> 
[/code]
当然,如果您是引用外部的css文件同样适用。' N7 k% ]% f  w8 k
. s/ j, M: v' g
另:可以将上面的方法简化为[code]function getDefaultStyle(obj,attribute){ // 返回最终样式函数,兼容IE和DOM,设置参数:元素对象、样式特性   : ", J( T2 e" [; E! O; d
return obj.currentStyle?obj.currentStyle[attribute]:document.defaultView.getComputedStyle(obj,false)[attribute];   . S/ {0 P# g. O/ _2 @
}
 [/code]

posted on 2009-05-21 21:09  yulei  阅读(5775)  评论(0编辑  收藏  举报