jQuery provides a nice, neat way to traverse the DOM...what I'm looking for is a way to traverse a stylesheet, getting and setting attributes for the defined styles.
Example Stylesheet
div {
background: #FF0000;
display: block;
}
.style {
color: #00AA00;
font-family: Verdana;
}
html body > nav.menu {
list-style: none;
}
Now imagine the following code is like jQuery for CSS...
Getting values from the CSS
$("div").attr("background");
//returns #FF0000;
$(".style").attr("color");
// returns #00AA00;
$("html body > nav.menu").attr("color");
// returns undefined;
Setting values in the CSS
$("div").attr("background", "#0000FF");
$(".style").attr("color", "#CDFF4E");
$("html body > nav.menu").attr("color", "#FFFFFF");
Fairly certain this is not possible...but just a wild stab in the dark!
jQuery provides a nice, neat way to traverse the DOM...what I'm looking for is a way to traverse a stylesheet, getting and setting attributes for the defined styles.
Example Stylesheet
div {
background: #FF0000;
display: block;
}
.style {
color: #00AA00;
font-family: Verdana;
}
html body > nav.menu {
list-style: none;
}
Now imagine the following code is like jQuery for CSS...
Getting values from the CSS
$("div").attr("background");
//returns #FF0000;
$(".style").attr("color");
// returns #00AA00;
$("html body > nav.menu").attr("color");
// returns undefined;
Setting values in the CSS
$("div").attr("background", "#0000FF");
$(".style").attr("color", "#CDFF4E");
$("html body > nav.menu").attr("color", "#FFFFFF");
Fairly certain this is not possible...but just a wild stab in the dark!
Share Improve this question asked Jan 8, 2014 at 0:34 Matthew LaytonMatthew Layton 42.5k58 gold badges209 silver badges338 bronze badges 7-
Using jQuerry, you can set CSS properties with
.css
method. See link – Daew Commented Jan 8, 2014 at 0:37 - @Daew, I know, but I don't want to set css properties on elements. I want to set/get them directly from the stylesheet. – Matthew Layton Commented Jan 8, 2014 at 0:38
- There's a nice tutorial here: davidwalsh.name/add-rules-stylesheets – Sam Dufel Commented Jan 8, 2014 at 0:39
- 2 What would the purpose of having get/set attributes and properties directly to the stylesheet using js? – Jason Commented Jan 8, 2014 at 0:40
- possible duplicate of Parsing CSS in JavaScript / jQuery – Mike Brant Commented Jan 8, 2014 at 0:41
2 Answers
Reset to default 7I think you can, but the interface is more obtuse than you probably want.
document.styleSheets
returns a StyleSheetList
object that seems to behave in an array like way.
So document.styleSheets[0]
returns a CSSStyleSheet
object. Look to have lots of ways to analyze it's content. And each CSSStyleSheet
has a cssRules
property which returns a CSSRuleList
.
And you can traverse the docs on the various types return by the DOM api from there yourself: https://developer.mozilla/en-US/docs/Web/API/CSSStyleSheet
I just found a way to look through all of your style sheets, using jquery initially:
I have three stylesheets on my page, so first, I must identify the one I need to manipulate and I gave it an id:
<style id="localRules">...</style>
Then, I use jQuery to initially find the id'd stylesheet I'm planning to change:
var sheetToChange = "localRules";
var sheets = $(document.styleSheets);
// loop through all the stylesheets
for (var thisSheet=0;thisSheet<sheets.length;thisSheet++){
// find the right stylesheet to work on
if(sheets[thisSheet].ownerNode.id == sheetToChange ){
// cross browser referencing of css rules:
var ruleSet = sheets[thisSheet].cssRules || sheets[thisSheet].rules;
for (var thisRule=0;thisRule<ruleSet.length;thisRule++){
// traverse in that style sheet for the rule you want to change, in this case, body:
if(ruleSet[thisRule].selectorText == "body"){
ruleSet[thisRule].style.cursor = "pointer";
}
}
break;
}
}
Hope this is helpful...it worked for me, but took a while to figure it out, especially because ownerNode
is something I've never heard of before.