In the process of using HTML to make a Web page, it is often necessary to dynamically change the properties of the control, which needs to be done by invoking JavaScript scripts. Here are some sample code that I've used.
- The JavaScript script changes the HTML properties of the page control.
1. Syntax:
document.getElementById (ID). attribute=New value
2. Description:
the HTML property of the Idattribute control for the ID control New Value the property value
3. Sample code:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">//function function: Change the Align property value of the control////parameter: The ID of the elem_id control//Alia_value Property ValuefunctionTxt_alig (elem_id,alig_value) {document.getElementById (elem_id). Align=alig_value;//Key Code}</script><div align= "center" ><input type= "button" value= "left" onclick= "Txt_alig (' txt ', ' ieft ')" > <input type= "button" value= "Middle" onclick= "Txt_alig (' txt ', ' center ')" ><input type= "button" value= "right" onclick= "Txt_alig (' txt ', ' right ')" ></div></body>
- The JavaScript script changes the CSS properties of the page space.
1. Syntax
document.getElementById (ID). style.property=New style
2. Description
The difference between changing the CSS property of a control with JavaScript and changing the HTML property is to add a style to the front of the property.
the Idproperty control's CSS property for the ID control New Value the property value
3. Sample Code
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">#color_box {background-color: #0F0; width:100px; height:100px; Margin-left:auto; Margin-right:auto; margin-top:200px;}</style><body><div id= "Color_box" align= "center" ></div><script language= "JavaScript" >functionChange_color (colo) {document.getElementById ("Color_box"). style.backgroundcolor=Colo;}</script><div align= "center" ><input type= "button" value= "Red" onclick= "Change_color (' #FF0000 ')"/> <input type= "button" value= "green" onclick= "Change_color (' #00FF00 ')"/><input type= "button" value= "Blue" onclick= " Change_color (' #0000FF ') "/></div></body>Using JavaScript to change the properties of a page element