11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

<br />

<br />

As you can see in Figure 15-5, the text remains underlined even though we‘ve set the inline<br />

style property to ―none.‖ <strong>The</strong> reason is that there is a CSS rule in the document-wide style<br />

sheet that overrides the inline setting using !important. However, alerting the style value<br />

clearly shows that the value for textDecoration is none, which is somewhat confusing.<br />

Figure 15-5: Computed style and actual style may vary<br />

Getting the actual style applied to an object can be tricky. In DOM2-compliant browsers, you<br />

can use the getComputedStyle() method of the document‘s default view. A document‘s default<br />

view is its default representation in the Web browser, that is, its appearance once all style rules<br />

have been applied. <strong>The</strong> getComputedStyle() method takes two arguments: a node for which<br />

style should be gotten and the pseudo-element (e.g., ―:hover‖) of interest (or the empty string<br />

for the normal appearance). You might get the style of the paragraph in the previous example<br />

with<br />

var p = document.getElementById("para");<br />

var finalStyle = document.defaultView.getComputedStyle(p, "");<br />

To examine individual properties, use the getPropertyValue() method, which takes a string<br />

indicating the property of interest:<br />

alert("<strong>The</strong> paragraph's actual text decoration is: " +<br />

finalStyle.getPropertyValue("text-decoration"));<br />

Unfortunately, as you get into the more esoteric aspects of DOM2, browser support varies<br />

significantly from vendor to vendor. Even worse, under IE6 and earlier you won‘t find support<br />

for this approach but instead will be required to use currentStyle to calculate an object‘s<br />

current property values. We present an example that works both with the proprietary and DOM<br />

syntax here.<br />

<br />

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

Saved successfully!

Ooh no, something went wrong!