18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

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

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

Advanced DOM Techniques<br />

<br />

<br />

onmouseout=”this.style.backgroundColor = ‘red’”><br />

When you move the mouse over the red , it changes to blue; when you mouse out, it returns to<br />

red. Note that the event handlers use the this keyword to refer to the itself and gain access to<br />

its style object.<br />

The style object also has a property called cssText that contains the CSS string describing the style of<br />

the element:<br />

<br />

<br />

Style Example<br />

<br />

<br />

<br />

<br />

<br />

When you click on the in this example, the text “background-color: red; height: 50px;<br />

width: 50px” displays.<br />

DOM style methods<br />

The DOM also described several methods for the style object, all designed to interact with individual<br />

parts of the CSS style definition:<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

getPropertyValue(propertyName) — Returns the string value of the CSS property<br />

propertyName. The property must be specified in CSS style, such as “background-color”<br />

instead of “backgroundColor”.<br />

getPropertyPriority() — Returns the string “important” if the CSS property<br />

“!important” is specified in the rule; otherwise it returns an empty string<br />

item(index) — Returns the name of the CSS property at the given index, such as<br />

“background-color”<br />

removeProperty(propertyName) — Removes propertyName from the CSS definition<br />

setProperty(propertyName, value, priority) — Sets the CSS property propertyName<br />

to value with the given priority (either “important” or an empty string)<br />

Here’s a simple example:<br />

<br />

<br />

Style Example<br />

<br />

function useMethods() {<br />

305

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

Saved successfully!

Ooh no, something went wrong!