I've been playing with document.styleSheets API and it mostly does what I want, but once I add a bunch of rules I want to be able to parse the shylesheet object into a valid css (so I can put it into a downloadable file), is possible with the API or just by manually looping trough it?
asked Jul 21, 2014 at 12:18
-
jigsaw.w3.org/css-validator/documentation.html Try thisGibbs– Gibbs2014年07月21日 12:26:49 +00:00Commented Jul 21, 2014 at 12:26
1 Answer 1
Iterate over it:
var rules = [], i, j, css;
for (i = 0; i < document.styleSheets.length; i++) {
for (j = 0; j < document.styleSheets[i].rules.length; j++) {
rules.push(document.styleSheets[i].rules[j].cssText);
}
}
css = rules.join('\n');
console.log(css);
answered Jul 21, 2014 at 12:37
1 Comment
user2579512
Is there an efficient way to output all styles under same selector, for example if I have rule1: header { color:red }, rule2: header { padding: 0 }, in the parsed file it would output: header {color:red; padding: 0}
lang-js