23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 8 ■ SCRIPTING CSS<br />

Additionally, four CSS-only members come from the CSSStyleSheet interface:<br />

cssRules<br />

ownerRule<br />

deleteRule()<br />

insertRule()<br />

Internet Explorer, of course, deviates from the DOM standard but not by much for StyleSheet.<br />

Internet Explorer renames ownerNode, which refers to the or element for the style sheet, as<br />

owningElement. For the other StyleSheet members, Internet Explorer abides by the DOM names.<br />

On the other hand, Internet Explorer does not implement any members from CSSStyleSheet. But<br />

don’t worry. There are Internet Explorer–only members that will enable us to muddle through. The ones<br />

we will explore are as follows:<br />

rules<br />

imports<br />

addRule()<br />

addImport()<br />

removeRule()<br />

removeImport()<br />

cssText<br />

Things could be worse, you know. Internet Explorer deviating from DOM makes scripting style<br />

sheets tough. But the proprietary workarounds make it doable. So, rather than feel sorry for ourselves,<br />

let’s start moving forward in small steps.<br />

Before we can query a rule, we need to get at the style sheet that contains the rule. One way is to<br />

query the sheet member of a or element in Firefox, Safari, and Opera. Internet Explorer<br />

deviates from DOM, no surprise, and implements a proprietary styleSheet member instead. With this in<br />

mind, our first steps will be the following:<br />

var myStyleSheet = document.getElementsByTagName("link")[0];<br />

myStyleSheet = myStyleSheet.sheet || myStyleSheet.styleSheet;<br />

Another, less reliable way to query a style sheet is by way of document.styleSheets, which contains<br />

an array-like object with one member for every or element having a rel attribute value<br />

set to "stylesheet". Internet Explorer and Opera also add one member for every or <br />

element having a rel attribute value set to "alternate stylesheet". No browser adds imported style<br />

sheets to document.styleSheets—just those included with a or embedded in a . Anyway,<br />

we could rewrite the previous sample like so:<br />

var myStyleSheet = document.styleSheets[0];<br />

■ Note document.styleSheets is provided by the DocumentStyle interface. In turn, styleSheets contains an<br />

array-like object provided by the StyleSheetList interface. Members contained by a StyleSheetList are objects<br />

that have the features listed in the StyleSheet and CSSStyleSheet interfaces. So, there are four interfaces for<br />

you to consult in a DOM reference.<br />

Now myStyleSheet contains an object representing eight.css. Let’s find the rule for the blue class.<br />

Where would that be? It’s in a cssRules member for Firefox, Safari, and Opera, but in a rules member for<br />

321

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!