Skip to content

Instantly share code, notes, and snippets.

@keenwon
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keenwon/603980ef8a27317eb430 to your computer and use it in GitHub Desktop.
Save keenwon/603980ef8a27317eb430 to your computer and use it in GitHub Desktop.
原生js获取元素样式
function getCss (el, cssName) {
if (el.style[cssName]) {
return el.style[cssName];
} else if (el.currentStyle) {
return el.currentStyle[cssName];
} else if (document.defaultView && document.defaultView.getComputedStyle) {
cssName = cssName.replace(/([A-Z])/g, '-$1');
cssName = cssName.toLowerCase();
var s = document.defaultView.getComputedStyle(el, '');
return s && s.getPropertyValue(cssName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment