Created
December 2, 2011 18:04
-
-
Save shawndumas/1424210 to your computer and use it in GitHub Desktop.
Set Element's Style
This file contains hidden or 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
| //setStyle( | |
| // document.getElementById('divId'), | |
| // 'height: 180px; text-align: left; overflow-y: scroll;' | |
| //); | |
| var setStyle = function (elm, style) { | |
| var rules = (function (style) { | |
| var css, v, | |
| rules = {}, | |
| div = document.createElement('div'); | |
| div.innerHTML = '<div style="' + style + '"></div>'; | |
| css = div.childNodes[0].style; | |
| for (var i in css) { | |
| ( | |
| i !== 'cssText' && | |
| (typeof i === 'string' || i instanceof String) | |
| ) && | |
| (v = css[i]) && | |
| (rules[i] = v); | |
| } | |
| return rules; | |
| })(style); | |
| for (var rule in rules) if (rules.hasOwnProperty(rule)) { | |
| elm.style[rule] = rules[rule]; | |
| } | |
| return elm; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment