只有 IE 和 Opera 支持使用 currentStyle 获取 HTMLElement 的计算后的样式
标准参考
DOM-2 Style 描述了在 ViewCSS 接口中使用 getComputedStyle 获取一个元素计算后的样式(即受该元素的默认样式、HTML 属性、样式表规则和行内样式影响后的最终计算样式)的方法,其语法为:
CSSStyleDeclaration getComputedStyle(in Element elt, in DOMString pseudoElt);
该方法的第一个参数为要获取计算后的样式的目标元素,第二个参数为伪元素。该方法返回一个 CSSStyleDeclaration 对象,通过 CSSStyleDeclaration 可以获取该元素的某种样式计算后的值。
关于 getComputedStyle 的详细信息,请参考 DOM-2 Style Interface ViewCSS 中的内容。
关于 CSSStyleDeclaration 的详细信息,请参考 DOM-2 Style Interface CSSStyleDeclaration 中的内容。
问题描述
只有 IE 和 Opera 支持使用 currentStyle 获取 HTMLElement 的计算后的样式,其他浏览器中不支持。
造成的影响
该问题将造成某些使用本特性设计的功能在非 IE 和 Opera 浏览器中不能实现。
受影响的浏览器
| IE6 IE7 IE8 Opera |
|---|
问题分析
获取元素计算后的样式在各浏览器中的实现有差异,IE 和 Opera 中可以直接使用 HTMLElement.currentStyle 来获取,但其他浏览器不支持,除 IE 外,其他浏览器都使用了 W3C 标准提倡的 getComputedStyle 方法来实现这个功能。
1. currentStyle
HTMLElement.currentStyle 是 IE 的特性,Opera 也实现了该特性。
运行以下代码:
typeof document.body.currentStyle
在各浏览器下的结果:
| IE6 IE7 IE8 Opera | Firefox Safari Chrome |
|---|---|
| object | undefined |
HTMLElement.currentStyle 返回一个 currentStyle 对象,通过该对象的属性,可以获取该元素的某种样式计算后的值:
- 在 currentStyle 对象中获取 CSS 样式的值时,要使用该样式的脚本名称,而不是该样式在 CSS 规则中的本来名称,如 'list-style-type' 要写为 'listStyleType'。
关于 currentStyle 的更多信息,请参考 MSDN currentStyle Object 中的内容。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style type="text/css">
body {
color: blue;
}
h1 {
font-size: 18px;
}
</style>
<script type="text/javascript">
window.onload=function(){
var $header=document.getElementById("header");
alert($header.currentStyle.fontWeight);
alert($header.currentStyle.fontSize);
alert($header.currentStyle.color);
}
</script>
</head>
<body>
<h1 id="header" style="color:red;">Header 1</h1>
</body>
</html>
在各浏览器下的结果:
| IE6 IE7 IE8 Opera | Firefox Safari Chrome |
|---|---|
| 700 18px red |
脚本出错 |
可见,Firefox Chrome Safari 不支持 currentStyle。
2. getComputedStyle
getComputedStyle 是 W3C 建议的方式,目前,Firefox Chrome Safari Opera 均实现了这一方法,调用 window.getComputedStyle 即可使用此方法。
运行以下代码:
typeof window.getComputedStyle;
在各浏览器下的结果:
| IE6 IE7 IE8 | Firefox Safari Chrome Opera |
|---|---|
| undefined | function |
getComputedStyle 返回一个 CSSStyleDeclaration 对象,通过该对象的属性或 'getPropertyValue' 方法,可以获取该元素的某种样式计算后的值:
- 在 CSSStyleDeclaration 对象中通过属性获取 CSS 样式的值时,要使用该样式的脚本名称,而不是该样式在 CSS 规则中的本来名称,如 'list-style-type' 要写为 'listStyleType'。
- 在 CSSStyleDeclaration 对象中通过 'getPropertyValue' 方法获取 CSS 样式的值时,要使用该样式在 CSS 规则中的本来名称,如 'list-style-type'、'font-weight' 等。
- getComputedStyle 方法的第一个参数为要获取计算后的样式的目标元素,第二个参数为期望的伪元素,如 ':after',':first-letter' 等,而不是伪类如 ':hover' 等。在 Firefox 中,第二个参数是必须的,如果没有期望的伪元素要设置为 'null',这与规范的要求相符。在 Chrome Safari Opera 中,第二个参数如果为 'null' 则可以省略。
关于 Firefox 中对 getComputedStyle 的支持情况,请参考 MDC window.getComputedStyle 中的内容。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style type="text/css">
body {
color: blue;
}
h1 {
font-size: 18px;
}
</style>
<script type="text/javascript">
window.onload=function(){
var $header=document.getElementById("header");
alert(getComputedStyle($header,null).fontWeight);
alert(getComputedStyle($header,null).fontSize);
alert(getComputedStyle($header,null).color);
}
</script>
</head>
<body>
<h1 id="header" style="color:red;">Header 1</h1>
</body>
</html>
注:
1. 以上代码中的 getComputedStyle($header,null).fontWeight 可以替换为 getComputedStyle($header,null).getPropertyValue("font-weight")。
2. 某些浏览器的早期版本可能要使用 document.defaultView.getComputedStyle 来代替 getComputedStyle 方法。
在各浏览器下的结果:
| IE6 IE7 IE8 | Firefox Safari Chrome | Opera |
|---|---|---|
| 脚本出错 | bold 18px rgb(255, 0, 0) |
700 18px #ff0000 |
可见,IE6 IE7 IE8 不支持 getComputedStyle,Opera 的返回值与 Firefox Safari Chrome 有差异。
解决方案
要获取元素的某种样式计算后的值,请考虑所有浏览器的兼容性情况。如使用以下代码给不支持 getComputedStyle 的 IE 提供与其他浏览器相同的函数:
if(!window.getComputedStyle){
window.getComputedStyle=function($target){
return $target.currentStyle;
};
}
然后在代码中使用 getComputedStyle(in Element elt, in DOMString pseudoElt) 来获取计算后的样式。但要注意:IE 中无法获取伪元素的计算样式。
浙公网安备 33010602011771号