Using JavaScript to change the properties of a page element

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.