Fungii said:
Pretty much all indexing in JavaScript starts with index 0
so
document.styleSheets[0]
is the first style sheet and
documeent.styleSheets[0].cssRules[0]
the first rule in the first style sheet.
You can also loop through the rules and check the
selectorText property.
Really? I thought Javascript was that weird language that began
their indexes at 1 instead of 0. My bad.
Alright thanks, that should do the trick!
Fungii
Maybe this will be of some help. Big disappointment: Opera v.7 tossed
in everything but the kitchen sink - and a document.styleSheets
collection. Just fyi.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"
http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>untitled</title>
<style type="text/css">
body {
height: 100%;
font-size: 100%;
}
p {
width: 400px;
margin: 160px auto;
font: normal 80% times, sans-serif;
color: darkgreen;
text-align: justify;
padding: 6px;
border: 1px #000 dashed;
background: #fafffa;
cursor: pointer;
}
p:first-letter {
font: bolder 120% times, sans-serif;
color: black;
}
</style>
<script type="text/javascript">
function modCSS(selector /* [attribute/value pairs] */)
{
if ('undefined' != typeof document.styleSheets)
{
var nsheets = document.styleSheets.length,
re = new RegExp('\\b' + selector + '\\b', 'i'),
SS,
rtype,
rule,
rules,
nrules;
for (var nsheet = 0; nsheet < nsheets; ++nsheet)
{
SS = document.styleSheets.item(nsheet);
rtype = ('undefined' != typeof SS.rules) ? 'rules' : 'cssRules';
if ('undefined' != typeof SS[rtype])
{
rules = SS[rtype];
nrules = rules.length;
for (nrule = 0; nrule < nrules; ++nrule)
{
rule = rules.item(nrule);
if (re.test(rule.selectorText))
{
for (var a = 1; a < arguments.length; a += 2)
rule.style[arguments[a]] = arguments[a + 1];
return;
}
}
}
}
}
}
window.onload = function()
{
document.onclick = function()
{
modCSS('p', 'color', 'black', 'fontSize', '90%');
modCSS('p:first-letter','color', 'green', 'fontSize', '160%');
modCSS('body', 'background', 'darkolivegreen');
}
}
</script>
</head>
<body>
<p title="click me">
Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
</p>
</body>
</html>