Accessing CSS Classes


document.getElementById("para").className =   "strong"; 

The most commonly used way to apply CSS to an HTML page is by using classes. With JavaScript, the class for every element can be accessed with the className property. The following code implements the preceding phrase with the class approach:

Changing the CSS Class (classname.html)

<script language="JavaScript"   type="text/javascript"> function makeBold() {   document.getElementById("para").className =     "strong";   window.setTimeout("makeLighter();", 1000); } function makeLighter() {   document.getElementById("para").className =     "weak";   window.setTimeout("makeBold();", 1000); } window.onload = makeBold; </script> <style type="text/css">   .strong { font-weight: bold; }   .weak { font-weight: lighter; } </style> <p >CSS and JavaScript</p> 

The preceding code changes the class for the text every second.




JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net