JS获取CSS属性值:getComputedStyle()
来源:互联网 发布:java目标字符串替换 编辑:程序博客网 时间:2024/05/16 23:51
在对网页进行调试的过程中,经常会用到js来获取元素的CSS样式,方法有很多很多,现在仅把我经常用的方法总结如下:
1. obj.style:这个方法只能JS只能获取写在html标签中的写在style属性中的值(style=”…”),而无法获取定义在<style type="text/css">里面的属性。
<!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″ /> <title>JS获取CSS属性值</title> <style type=”text/css”> <!– .ss{color:#cdcdcd;} –> </style> </head> <body> <div id=”css88″ class=”ss” style=”width:200px; height:200px; background:#333333″>JS获取CSS属性值</div> <script type=”text/javascript”> alert(document.getElementById(“css88″).style.width);//200px alert(document.getElementById(“css88″).style.color);//空白 </script> </body> </html>
2. IE中使用的是obj.currentStyle方法,而FF是用的是getComputedStyle 方法
“DOM2级样式”增强了document.defaultView,提供了getComputedStyle()方法。这个方法接受两个参数:要取得计算样式的元素和一个伪元素字符串(例如“:after”)。如果不需要伪元素信息,第二个参数可以是null。getComputerStyle()方法返回一个CSSStyleDeclaration对象,其中包含当前元素的所有计算的样式。以下面的HTML页面为例:
<!DOCTYPE html><html><head><title>计算元素样式</title><style>#myDiv { background-color:blue; width:100px; height:200px;}</style><body><div id ="myDiv" style="background-color:red; border:1px solid black"></div><script> var myDiv = document.getElementById("myDiv"); var computedStyle = document.defaultView.getComputedStyle(myDiv, null); alert(computedStyle.backgroundColor); //"red" alert(computedStyle.width); //"100px" alert(computedStyle.height); //"200px" alert(computedStyle.border); //在某些浏览器中是“1px solid black”</script></body></head></html>
边框属性可能也不会返回样式表中实际的border规则(Opera会返回,但其它浏览器不会)。存在这个差别的原因是不同浏览器解释综合属性的方式不同,因为设置这种属性实际上会涉及很多其他的属性。在设置border时,实际上是设置了四个边的边框宽度、颜色、样式属性。因此,即使computedStyle.border不会在所有浏览器中都返回值,但computedStyle.borderLeftWidth则会返回值。
需要注意的是,即使有些浏览器支持这种功能,但表示值的方式可能会有所区别。例如,Firefox和Safari会返回将所有颜色转换成RGB格式。因此,即使getComputedStyle()方法时,最好多在几种浏览器中测试一下。
IE不支持getComputedStyle()方法,但它有一种类似的概念。在IE中,每个具有style属性的元素还有一个currentStyle属性。这个属性是CSSStyleDeclaration的实例,包含当前元素全部计算后的样式。取得这些样式的方法差不多,如下:
var myDiv = document.getElementById("myDiv"); var computedStyle = myDiv.currentStyle; alert(computedStyle.backgroundColor); //"red" alert(computedStyle.width); //"100px" alert(computedStyle.height); //"200px" alert(computedStyle.border); //undefined
与DOM版本的方式一样,IE也没有返回border样式,因为这是一个综合属性。3. 我自己在写测试case过程中写的一个简单的函数(适用于Chrome):
function getCSS(div){ return document.defaultView.getComputedStyle(div, null);//return div.currentStyle;//没用过,IE }
参考:http://kingphp.blog.163.com/blog/static/20042324420120109438458/
http://www.w3cmm.com/dom/getcomputedstyle-currentstyle.html
- JS获取CSS属性值:getComputedStyle()
- JS使用getComputedStyle()方法获取CSS属性值
- 获取CSS属性值的方法getComputedStyle
- Javascript获取CSS属性值getComputedStyle和currentStyle
- js获取css值的方法:style、getComputedStyle和currentStyle
- js获取css值的方法:style、getComputedStyle和currentStyle
- js获取css值的方法:style、getComputedStyle和currentStyle
- 获取元素CSS值getComputedStyle()
- 获取元素CSS值之getComputedStyle方法
- javascript获取元素CSS值window.getComputedStyle
- 获取元素CSS值之getComputedStyle方法
- JS获取CSS样式(style/getComputedStyle/currentStyle)
- js中获取元素属性、currentStyle与getComputedStyle的区别
- JS获取CSS属性值
- JS获取CSS属性值
- JS获取CSS属性值
- JS获取CSS属性值
- JS获取CSS属性值
- C#将数据转换为指定类型,支持对可空类型(Nullable类)转换方法示例
- IT和烹饪,哪个更难?(1)
- 基于COM和.Net视图状态的Asp.net
- 自动升级出错:80072efe解决
- 第7周作业1——背包问题
- JS获取CSS属性值:getComputedStyle()
- IT和烹饪,哪个更难?(2)
- pscp -- Windows 与 Linux 之间文件传输
- gsoap 2.8.17 vc webservice
- 函数return时崩溃
- eclipse运行wordcount
- java ReadWriteLock锁
- 用gdb调试软件
- SQl Server利用bak文件还原数据库