In JavaScript, the classNam开发者_如何学Goe
property gives the class name of an HTML element, but how to get the contents of the class.
first you need to get the cssRules array but since it is not cross platform you should use a code like this:
if ( document.styleSheets[0].cssRules ) {
var cssRules = document.styleSheets[0].cssRules;
} else if ( document.styleSheets[0].rules ) {
var cssRules = document.styleSheets[0].rules;
}
now cssRules is an array of selectors in your stylesheet you can go through it like this:
for( var i = 0; i < cssRules.length; ++i ) {
if( cssRules[i].selectorText == '.myClass' ) {
alert( cssRules[i].style.color );
}
}
The actual style rules are stored in the style
property.
Also, jQuery has some really nice methods for working with style. If you want to explore that route, check out the css
method.
EDIT: Just re-read the question and realized that the OP was asking about the style rules for the class, not the style rules for the element. However, because the context isn't exactly clear, I'll let this answer stand for now, as it may still be helpful.
精彩评论