This article is a simple example of modifying a BUTTON element property (for example: adding a property, modifying a property, modifying a color style, border style, and so on), and demonstrates JS's basic method of modifying HTML element properties, see. If you have any shortcomings, please correct me and appreciate it!
More than idle statement, this on the code.
The HTML source code is as follows:
1 <HTML>2 <Head>3 <MetaCharSet= ' Utf-8 '>4 5 <title>Js-002-javascript manipulating common web elements-modifying element properties (example of a button)</title>6 <Scripttype= "Text/javascript"src= "Common.js"></Script>7 <Scripttype= "Text/javascript"src= "Test.js"></Script>8 9 <Linkrel= "stylesheet"type= "Text/css"href= "Global.css">Ten </Head> One A <Body> - <DivID= "button"> - <h4>Button operation:</h4> the <Li>var btn = document.getElementById (' button '). getElementsByTagName (' input ') [0]</Li> - <Li>1. Add attribute: btn.id= "BTN"</Li> - <Li>2. Add attribute: Btn.setattribute ("name", "Btn_name")</Li> - <Li>3. Add attribute: Btn.setattribute ("Class", "A_class"), class cannot be added with the first method</Li> + <Li>4, modify the font color: Btn.style.color = ' red '</Li> - <Li>5. Modify the Border style: Btn.style.border = ' 3px solid blue '</Li> + <BR> A at <inputtype= "button"value= "JavaScript action Button instance"onclick= "Btn_op ()"> - </Div> - - <Div> - <BR><BR><BR><BR> - <ahref= "Http://www.w3school.com.cn/jsref/dom_obj_style.asp"Target= "_blank">HTML DOM Style Object</a> in </Div> - </Body> to </HTML>
The following is shown in Chrome:
The corresponding invocation of the JS source code is as follows:
1 //button Action: Add Property2 functionBtn_op () {3 varBTN = document.getElementById (' button '). getElementsByTagName (' input ') [0];4 5Btn.id= "BTN";6Btn.setattribute ("name", "Btn_name");7Btn.setattribute ("Class", "A_class");8 9 //Modify the value of the property, just re-assign the value you want toTenBtn.setattribute ("Class", "a_class-01"); One ABtn.style.color = ' Red '; -Btn.style.border = ' 3px solid blue '; -}
When you click the button, the page appears as follows:
Other HTML element properties of the modification method and the above are the similarities, the small master can make a further attempt on their own, thank you!
At this point, the js-002-javascript operation of the common web elements of the second-modify element properties (with the button example) successfully completed, I hope this article can give beginners JavaScript you a reference.
Finally, very grateful to the pro-stop, I hope this article can be pro helpful. Warmly welcome the kiss to discuss together and progress together. Thank you so much! ^_^
Js-002-javascript manipulating common web elements-modifying element properties (example of a button)