Last active
January 26, 2017 10:46
-
-
Save gustinmi/204c5c7c268ece7f4d316fb474f7cb7a to your computer and use it in GitHub Desktop.
Get css rule content with javascript http://stackoverflow.com/questions/324486/how-do-you-read-css-rule-values-with-javascript
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
var myStyle = function getStyle(className_) { | |
var styleSheets = window.document.styleSheets; | |
var styleSheetsLength = styleSheets.length; | |
for(var i = 0; i < styleSheetsLength; i++){ | |
var classes = styleSheets[i].rules || styleSheets[i].cssRules; | |
if (!classes) | |
continue; | |
var classesLength = classes.length; | |
for (var x = 0; x < classesLength; x++) { | |
if (classes[x].selectorText == className_) { | |
var ret; | |
if(classes[x].cssText){ | |
ret = classes[x].cssText; | |
} else { | |
ret = classes[x].style.cssText; | |
} | |
if(ret.indexOf(classes[x].selectorText) == -1){ | |
ret = classes[x].selectorText + "{" + ret + "}"; | |
} | |
return ret; | |
} | |
} | |
} | |
}; | |
myStyle("body") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment