styleSheet object
Represents a CSS style sheet that belongs to a style element or an imported style file.
Style files can be imported by a link element or with the import style rule.
With styleSheet objects you can access rules and at-rules, modify the contents of style sheets and navigate through the style sheet hierarchy.
With styleSheet objects you can access rules and at-rules, modify the contents of style sheets and navigate through the style sheet hierarchy.
Syntax:
Properties that reference the object:
+ | object.parentStyleSheet |
+ | object.sheet |
+ | object.styleSheet |
| CSSImportRule.styleSheet |
Methods that return the object:
| styleSheets.item (index) |
| imports.item (index) |
The base interface, through which you can add new functionalities to the styleSheet object, is the CSSStyleSheet interface.
Possible members:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Example HTML code 1:
This example shows how to change a style rule in an embedded style sheet:
|
||||
<head> <style id="myStyle"> .red { color: red; } </style> <script type="text/javascript"> function ChangeRedColor () { var styleTag = document.getElementById ("myStyle"); // the style sheet in the style tag var sheet = styleTag.sheet ? styleTag.sheet : styleTag.styleSheet; // the first rule in the style sheet var rules = sheet.cssRules ? sheet.cssRules : sheet.rules; var firstRule = rules[0]; // change the color firstRule.style.color = "#00ff00"; } </script> </head> <body> <button onclick="ChangeRedColor ()">Change the color of the '.red' style rule!</button> <br /><br /> <div class="red">red division</div> <div>non-red division</div> <span class="red">red span</span> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example shows how to change a style rule in an external style sheet:
|
|||||
<head> <link id="myLink" rel="stylesheet" type="text/css" href="red.css" /> <script type="text/javascript"> function ChangeRedColor () { var linkTag = document.getElementById ("myLink"); // the imported style sheet var importedSheet = linkTag.sheet ? linkTag.sheet : linkTag.styleSheet; // the first rule in the style sheet var rules = importedSheet.cssRules ? importedSheet.cssRules : importedSheet.rules; var firstRule = rules[0]; // change the color firstRule.style.color = "#00ff00"; } </script> </head> <body> <button onclick="ChangeRedColor ()">Change the color of the '.red' style rule!</button> <br /><br /> <div class="red">red division</div> <div>non-red division</div> <span class="red">red span</span> </body> |
|||||
|
|||||
Did you find this example helpful?
|
Related pages:
External links:
User Contributed Comments