Changing HTML Properties
To change the attributes of an HTML element, use this syntax:
| The code is as follows |
Copy Code |
| document.getElementById (ID). attribute=new value |
This example changes the SRC attribute of the element:
| The code is as follows |
Copy Code |
<script type= "Text/javascript" > document.getElementById ("image"). src= "Landscape.jpg"; </script>
|
Change HTML Style
To change the style of HTML elements, use this syntax:
| The code is as follows |
Copy Code |
document.getElementById (ID). style.property=new style
|
The following example will change
The style of the element:
Hello world!
| The code is as follows |
Copy Code |
<script type= "Text/javascript" > document.getElementById ("P2"). style.color= "Blue"; </script> |
document.getElementById (...) ' is empty or not an object
| The code is as follows |
Copy Code |
| <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <title> Untitled page </title> <style type= "text/css" body { font-family: "Song body"; FONT-SIZE:12PX; } </style> <script language= "JavaScript type=" Text/javascript document.getElementById ("Divtest"). Innerhtml= "Hello World"; / When the page finishes loading this JavaScript code, it starts running, but the following ID divtest div tag is not loaded, so it cannot be found </script> </ Head> <body> <div id= "divtest" </div> </body> |