Creates a new style rule for the styleSheet object, and returns the index into the rules collection.
Syntax
styleSheet.addRule(sSelector, sStyle [, iIndex])
Parameters
sSelector Required. String that specifies the selector for the new rule. Single contextual selectors are valid. For example, "DIV P B" is a valid contextual selector. sStyle Required. String that specifies the style assignments for this style rule. This style takes the same form as an inline style specification. For example, "color:blue" is a valid style parameter. iIndex Optional. Integer that specifies the location in the rules collection to add the new style rule. If an index is not provided, the rule is added to the end of the collection by default.
Return Value
The return value is reserved; do not use.
Remarks
You can apply rules to a disabled styleSheet, but they do not apply to the document until you enable the styleSheet.
Example
This example uses the addRule method to add a rule that sets all bold text appearing in a DIV to the color blue.
<DIV> Internet Explorer makes <B>HTML</B> dynamic. </DIV> <SCRIPT> var new_rule; new_rule = styleSheets[0].addRule("DIV B", "color:blue", 0); </SCRIPT>This example uses the addRule method to add two rules to the end of the rules collection. The rules apply the hover and link pseudo-class attributes to all anchors that appear within an H2 heading.
<H2> <A HREF="http://www.microsoft.com/"> Where Do You Want to Go Today? </A> </H2> <SCRIPT> document.styleSheets[0].addRule("H2 A:hover", "color:gold"); document.styleSheets[0].addRule("H2 A:link", "color:black"); </SCRIPT>
Applies To
See Also
removeRule, rules, styleSheets