|
September 24, 2001 Finding Style Rule Objects Tips: September 2001
Yehuda Shiran, Ph.D.
|
|
You can access style definitions in their object form. The STYLE tags are represented in a zero-based collection, document.styleSheets. Each STYLE definition may include one or more rules. Each STYLE definition's rules are represented as a zero-based collection, document.styleSheets[i].rules. The style rule's name is stored as a property of the rules[j] object. The property name is selectorText. If your page looks like this:
then you'll have four style rule objects: document.styleSheets[0].rules[0], document.styleSheets[0].rules[1], document.styleSheets[0].rules[2], and document.styleSheets[0].rules[3]. Their corresponding rule names can be accessed as follows:
The values of these properties are ".contentstyle", ".masterstyle", ".headerstyle", and ".footerstyle".
Suppose you want to store the above four objects in four object variables, oMasterStyleClass, oContentStyleClass, oFooterStyleClass, and oHeaderStyleClass, respectively. One way to do it is to go over all rule objects and search for the one you need, by name. The following findStyleRule() function does exactly that. It accepts a style rule name and returns the style rule object, styleSheets[i].rules[j]:
The second line below returns the style rule object associated with the .masterstyle rule:
People who read this tip also read these tips: Look for similar tips by subject: |