www.fgks.org   »   [go: up one dir, main page]

Click to Rate and Give Feedback
HTML and DHTML Overviews and Tutori...
 Changing Element Styles
Changing Element Styles

Dynamic HTML (DHTML) allows you to write script that can change the HTML attributes that modify a given element. These attributes can be things such as the SRC attribute on an img or iframe element, or the style attribute for a div element. This document shows you how to change the SRC attribute on an iframe element so that the contents of iframe change on the fly. The same example also shows you how to change the background color of the body for the SRC= document of the iframe. The next set of examples shows how to change the class name of the element so that it is associated with a different style sheet.

DHTML gives you scriptable access to all HTML elements, attributes, and Cascading Style Sheets (CSS) styles. You as the author can use these techniques to design some pretty cool DHTML Web pages.

Changing the Style of an IFRAME

The following example shows how to change the contents of iframe as well as change the style attribute of the BODY element of the contained iframe. Click the Show Me button to show the page, and then look at the code.

<HTML>
<HEAD><TITLE>Changing Element Attributes</TITLE>
<SCRIPT LANGUAGE="JavaScript">
 function change_image() {
   document.all.myiframe.src="frame_content2.htm";  
   //use the all collection to access attributes of the IFRAME element
}
function change_background() {
     document.frames.myiframe.document.body.style.backgroundColor="green";   
     // use the frames collection to access the document contained in the IFRAME.
}
</SCRIPT>
</HEAD>
<BODY>
    <IFRAME id=myiframe src="frame_content.htm">
    </IFRAME>
    <BUTTON  contents of the IFRAME</BUTTON>
    <BUTTON  the background color of the IFRAME</BUTTON>
</BODY>
</HTML>
This feature requires Microsoft® Internet Explorer 4.0 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

Changing Element Classes

There are two primary ways to change the inline style for an element—either change the element's style attribute, or change the className of the element to match a defined class in a global style sheet. The following example shows how straightforward it is to change the inline style attribute with an event.

<H1 id=myStyle >This text will change to blue when clicked </H1>

This changes the property "color" of the style sub-object for the H1 element.

However, to change the style for more than one element, use global style sheets, which let you define styles based on a "contextual selector." Styles can apply to tag names, element identifiers, or all elements in a class. The DHTML class attribute on every element lets you associate any element with any class name. This way, multiple elements can belong to a single class, and setting a global style for a class means that the style sheet applies to every element with that class name.

Because the class attribute can be scripted, you can dynamically change class associations with elements in a document, as in the following example.

<HTML>
<HEAD><TITLE>Changing Element Attributes</TITLE>
<STYLE>
.textRed {color:red}
.textBlue {color:blue}
</STYLE>
</HEAD>
<BODY>
<H1 class=textBlue 
    >
Mouse over this text to see the color change from blue to red and back to blue
</H1>
</BODY>
</HTML>
This feature requires Microsoft® Internet Explorer 5.5 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

In the example, global rules are defined for two classes, textRed and textBlue. The H1 initially is a part of the textBlue class and therefore is displayed in blue. Moving the mouse over the text fires the onmouseover event, and the inline event handler changes the association of the H1 element to the textRed class, which renders the text red. Note that the class attribute is referenced through the className property.

Note  The scriptable properties for CSS styles are all read/write except position. You cannot dynamically set a nonpositioned element to be positioned. If you want to absolutely position an element using an event handler script, be sure that the element has its initial position set to relative using either a global style sheet or an inline style.

Scripting CSS Attributes

All style attributes are strings. Several properties have been added to the style object to help manipulate values as nonstrings. For example, pixelHeight is an integer so that the height of an object can be mathematically manipulated in base pixel units regardless of how the height attribute was set. The posHeight property is an integer that describes the height attribute in the units to which the author originally set the CSS height attribute.

If a style property represents some number of units, you can include a units designator when assigning the value. The pt (points) and px (pixels) designators are used in the following examples.

document.all.MyElement.style.fontSize = "24pt";
document.all.MyElement.style.fontSize = "120px";

If you don't supply a units designator, the default unit of measure for the property is used. One benefit of this is that you can assign a number or the result of an expression (rather than a string) and it will be converted automatically. For example, the assignments below are the same—they both set the font size to 72 pixels.

document.all.MyElement.style.fontSize = "72";
document.all.MyElement.style.fontSize = 72;
Tags What's this?: html (x) s1 (x) Add a tag
Community Content
 
Add Community Content
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker
DCSIMG