using JavaScript to rewrite CSS rules

J

jbj

I am trying to make a javascript function that can rewrite javascript
rules dynamically (no, not the style tag of an individual element, but
the actual rule in the <head> portion of the document).

I have this code working in Mozilla (so I imagine Netscape browsers)
but it gives me an error with IE. A few questions about it:

First, here is my current code:
<code>
function expand(show)
{
for(var i=1;i<=linkNum;i++)
{
if(show == i)) document.styleSheets[0].cssRules.style.visibility="visible";
else document.styleSheets[0].cssRules.style.visibility="hidden";
}
}
</code>

As I said, this works in Moz, but not in IE. How do i handle this??

And my next question, instead of having to access each rule by its
index in an array of the stylesheet, is there a way I can access it by
name (so instead of doing cssRules[0], i could do.Rule("body") or
something with a similar function?)

I am trying to do some work with more advanced DHTML and am running
into even more IE specfic code it seems. Thanks for the help.
 
M

Martin Honnen

jbj said:
I am trying to make a javascript function that can rewrite javascript
rules dynamically (no, not the style tag of an individual element, but
the actual rule in the <head> portion of the document).

I have this code working in Mozilla (so I imagine Netscape browsers)
but it gives me an error with IE. A few questions about it:

First, here is my current code:
<code>
function expand(show)
{
for(var i=1;i<=linkNum;i++)
{
if(show == i)) document.styleSheets[0].cssRules.style.visibility="visible";
else document.styleSheets[0].cssRules.style.visibility="hidden";
}
}
</code>

As I said, this works in Moz, but not in IE. How do i handle this??


IE has its own API, with IE you need e.g.
document.styleSheets[0].rules[1].style.cssPropertyName = ...
And my next question, instead of having to access each rule by its
index in an array of the stylesheet, is there a way I can access it by
name (so instead of doing cssRules[0], i could do.Rule("body") or
something with a similar function?)

You can check the selectorText property of a rule.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top