"Andrew Poulos" wrote
If I know the name of an imported stylesheet, and I know the name of a
specific selector, and I know the name of the property, how can I get
the corresponding value? In the following case:
<style type="text/css">@import url(test.css);</style>
which contains (among other things)
.tester {
color: red;
}
I know the CSS is 'test.css', the selector is the class '.tester', and
the property is 'color'. I need to get the value, in this case 'red',
or, say, null if an error occurred.
Hopefully this can be of help:
function getcss( selector, property ) {
var i, r, s=document.styleSheets && document.styleSheets[0]; if(s) {
r = s.rules ? s.rules : s.cssRules; if(r) {
i = r.length; while (i--) {
if(r[i].selectorText.toLowerCase() === selector.toLowerCase()) {
return ( r[i].style[property] );
}
}
}
}
return null;
}
alert( getcss( '.myclass', 'fontSize' ) );
You could expand the function to loop through all stylesheets, it currently
only looks through the first one. The .toLowerCase() statements make the
function case-insensitive, so you had better not name your classes .such and
..SUCH (for example). This is because IE turns all tagnames in the stylerules
to uppercase.
hth
Ivo
http://4umi.com/web/javascript/