惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
MongoDB | Blog
MongoDB | Blog
The Register - Security
The Register - Security
T
The Blog of Author Tim Ferriss
A
About on SuperTechFans
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Jina AI
Jina AI
Stack Overflow Blog
Stack Overflow Blog
Cisco Talos Blog
Cisco Talos Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
W
WeLiveSecurity
S
Securelist
I
Intezer
F
Full Disclosure
WordPress大学
WordPress大学
腾讯CDC
酷 壳 – CoolShell
酷 壳 – CoolShell
Latest news
Latest news
aimingoo的专栏
aimingoo的专栏
C
Cisco Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
K
Kaspersky official blog
阮一峰的网络日志
阮一峰的网络日志
P
Proofpoint News Feed
J
Java Code Geeks
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
AWS News Blog
AWS News Blog
T
Tenable Blog
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
L
LINUX DO - 最新话题
小众软件
小众软件
T
Threat Research - Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
The GitHub Blog
The GitHub Blog
爱范儿
爱范儿
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
量子位
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Forbes - Security
Forbes - Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
O
OpenAI News
V
V2EX
T
Troy Hunt's Blog

博客园 - yulei

几个css技巧 HttpWebRequest 发送 POST实现自动用户登录 - yulei jquery selector 基础 - yulei asp.net ajax实现:Jquery+Json - yulei - 博客园 smtp协议 发送带附件的邮件 控制滚动条 (转)防数据库批量注入JS HttpCompress相关问题解决方法 - yulei - 博客园 C#发送Email邮件方法总结 数据导出到excel文件给客户端下载的几种方法(转) ndoc2007,生成注释文档,支持泛型,2.0,中文注解,部分汉化 IE和Firefox浏览器CSS网页布局不同点 - yulei - 博客园 通用获得网页的实际大小的javascript函数 仿DVBBS下拉菜单效果 webClient下载进度条 asp.net下URL网址重写成.html格式、RSS、OPML的知识总结 导出excel 使用AD724的RGB→NTSC/PAL制信号转换电路 AV-RF转换器的制作
转载 JS获取CSS属性值 - yulei - 博客园
yulei · 2009-05-21 · via 博客园 - yulei

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]