04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 11: DOM Levels 2 and 3<br />

Assuming that this rule is in the first style sheet on the page and is the only style in that style sheet, the<br />

following code can be used to retrieve all of its information:<br />

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

var rules = sheet.cssRules || sheet.rules;<br />

var rule = rules[0];<br />

alert(rule.selectorText);<br />

alert(rule.style.cssText);<br />

alert(rule.style.backgroundColor);<br />

alert(rule.style.width);<br />

alert(rule.style.height);<br />

//get rules list<br />

//get first rule<br />

//”div.box”<br />

//complete CSS code<br />

//”blue”<br />

//”100px”<br />

//”200px”<br />

Using this technique, it ’ s possible to determine the style information related to a rule in the same way<br />

you can determine the inline style information for an element. As with elements, it ’ s also possible to<br />

change the style information, as shown in the following example:<br />

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

var rules = sheet.cssRules || sheet.rules;<br />

var rule = rules[0];<br />

rule.style.backgroundColor = “red”<br />

//get rules list<br />

//get first rule<br />

Note that changing a rule in this way affects all elements on the page for which the rule applies. If there<br />

are two < div > elements that have the box class, they will both be affected by this change.<br />

Creating Rules<br />

The DOM states that new rules are added to existing style sheets using the insertRule() method. This<br />

method expects two arguments: the text of the rule and the index at which to insert the rule. Here is an<br />

example:<br />

sheet.insertRule(“body { background-color: silver }”, 0);<br />

//DOM method<br />

This example inserts a rule that changes the document ’ s background color. The rule is inserted as the<br />

first rule in the style sheet (position 0)—the order is important in determining how the rule cascades into<br />

the document. The insertRule() method is supported in Firefox, Safari, Opera, and Chrome.<br />

IE has a similar method called addRule() that expects two arguments: the selector text and the<br />

CSS style information. An optional third argument indicates the position in which to insert the rule.<br />

The IE equivalent of the previous example is as follows:<br />

sheet.addRule(“body”, “background-color: silver”, 0);<br />

//IE only<br />

The documentation for this method indicates that you can add up to 4,095 style rules using addRule() .<br />

Any additional calls result in an error.<br />

To add a rule to a style sheet in a cross - browser way, the following method can be used. It accepts four<br />

arguments: the style sheet to add to, followed by the same three arguments as addRule() , as shown in<br />

the following example:<br />

334

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

Saved successfully!

Ooh no, something went wrong!