转载地址: http://blog.youkuaiyun.com/hulefei29/article/details/3694884
function getCurrentStyle (obj, cssName) {
if (obj.currentStyle) {
return obj.currentStyle[cssName];
}
else if (window.getComputedStyle) {
cssName = cssName.replace (/([A-Z])/g, "-$1").toLowerCase ();
return document.defaultView.getComputedStyle(obj,null)[cssName];
}
return null;
}
obj.style方法,这个方法只能JS只能获取写在html标签中的写在style属性中的值(style="...").
IE中使用的是obj.currentStyle方法,而FF是用的是getComputedStyle 方法。4 b- L. ^/ @# b0 w- y5 X
另:可以将上面的方法简化为:
function getDefaultStyle(obj,attribute){ // 返回最终样式函数,兼容IE和DOM,设置参数:元素对象、样式特性
return obj.currentStyle? obj.currentStyle[attribute] : document.defaultView.getComputedStyle(obj,false)[attribute];
}