这个 Get the rendered style of an element 是2006年4月的24发的,但我今天才看到。
为了拿到元素的当前样式
ie 用 currentStyle 和 runtimeStyle
ff 用 getComputedStyle
opera 似乎是直接 style 和 getComputedStyle 都可以(记不清楚了,谅解)
以前都是自己写,再说公司的破项目仍然都在 ie 6 下跑,因此倒还没感到痛苦。
函数定义
function getStyle(oElm, strCssRule){
var strValue = “”;
if(document.defaultView && document.defaultView.getComputedStyle){
strValue = document.defaultView.getComputedStyle(oElm, “”).getPropertyValue(strCssRule);
}
else if(oElm.currentStyle){
strCssRule = strCssRule.replace(/-(w)/g, function (strMatch, p1){
return p1.toUpperCase();
});
strValue = oElm.currentStyle[strCssRule];
}
return strValue;
}
调用
getStyle(document.getElementById(”container”), “font-size”);
唯一缺点在于 ie 5,需要加 try catch
下载 getStyle.js
Leave a reply