Skip to content

Instantly share code, notes, and snippets.

@hehongwei44
Created July 27, 2014 03:04
Show Gist options
  • Save hehongwei44/9abf63536accd0f2eeb7 to your computer and use it in GitHub Desktop.
Save hehongwei44/9abf63536accd0f2eeb7 to your computer and use it in GitHub Desktop.
样式相关的通用函数
/**
* 获取指定元素(elem)的样式属性(name)
* */
function getStyle(elem, name) {
//如果存在于style[]中,那么它已被设置了(并且是当前的)
if (elem.style[name]) {
return elem.style[name];
}
//否则,测试IE的方法
else if (elem.currentStyle) {
return elem.currentStyle[name];
}
//或者W3C的方法
else if(document.defaultView && document.defaultView.getComputedStyle){
name = name.replace(/(A-Z)/g, "-$1");
name = name.toLowerCase();
var s = document.defaultView.getComputedStyle(elem, "");
return s && s.getPropertyValue(name);
}
//否则,用户使用的是其他浏览器
else {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment