|
|
Dynamic Content with DOM-2 (Part II of II) Manipulating Element StylesSo far, I've only talked about manipulating element attributes. Often, an element will employ CSS to give it a particular style or formatting. Altering the CSS properties of an element requires a similar yet slightly different approach. The DOM specification provides another interface called DOM Level 2 Style, which is for manipulating the CSS properties of an element. Each CSS rule for an element is represented as a property of the element's Because
Click the button below to view the style properties of the H2 element below. Note: some users of IE 5.1 Preview for Mac OS X have expirenced problems with this script. Headline HereYou'll notice that many of the style properties are empty, since we haven't defined them to be otherwise. You can set style properties directly. To read and then set the background color of an HTML page (or specifically, the background color of the BODY element):
Manipulating the CSS properties of elements is key to DHTML functionality. For example, you can use JavaScript to change the table's LEFT and BACKGROUND-COLOR CSS properties, thus changing its color and position on the page. Try it out:
Change Position Change Color: Here's the JavaScript that makes it happen. In all cases, the table ID,
To get the name of the scriptable style property, take the original style rule from the CSS declaration and capitalize the first letter following each hyphen, then remove the hyphens altogether. The following table contains a few examples:
From HereThis concludes our brief tour of the Document Object Model. We've touched on some of the more exciting features of the DOM, including dynamic element creation and setting element attributes and CSS properties through JavaScript. The DOM interface has many more features we didn't explore in these articles; I encourage you to check out the specification at the W3C site, specifically the ECMAScript bindings pages which describe the DOM Level 2 Core and Style scripting interface in more detail. Be sure to check your browser's documentation to see what DOM features it supports. Scott Andrew LePera lives in San Francisco, where he ekes out a schizoid existence as both a web applications developer for KnowNow and a frustrated urban folk singer. Previously in this series: Dynamic Content with DOM-2 (Part I of II) -- The DOM-2 is supported in both Mac IE5 and NS6, and it provides an interface that enables developers to generate dynamic content without being limited to JavaScript trickery. This article explores some of the basic functionality of DOM-2. Return to the JavaScript and CSS DevCenter. |
|
|
|
|
||||||||