Created
July 27, 2014 03:04
-
-
Save hehongwei44/9abf63536accd0f2eeb7 to your computer and use it in GitHub Desktop.
样式相关的通用函数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 获取指定元素(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