First, get the element
1. Find Elements by ID
How to obtain document:
The way to get elements by ID is as follows:
12 |
var idq = document.getElementById( "idq" ); alert(idq); |
2. Get elements based on class
12 |
var o_class1 = document.getElementsByClassName( "divclass" ); //(数组) alert(o_class1); |
3. Get elements according to tags
4. Find the element according to the name
var ss = Document.getelementsbyname ("ys") alert (ss[0]);
Name is multiple and plural.
Ii. Elements of Operation
1. Operation Contents
① Non-form elements
(1) Get text: InnerText
(2) Set text
(3) Get HTML code
If you want to get all the code in a div, including span:
(4) Set HTML code
Set the HTML code, is can increase the dynamic effect, output when the page display effect. Conversely, if you use InnerText, the effect (<b> bold </b>) is also displayed.
② form Elements
(1) Assign value (set a value)
var B1 = document.getElementById ("B1");// alert (value = "Please enter content"); Prompt box output b1.value = "Please enter content"; Web page Output
(2) Get Value (value)
alert (B.value);
2. Operation Properties
① adding properties (Settings)
Add attribute var d1 = document.getElementById ("IDQ"); D1.setattribute ("Style", "color:red");
② Getting properties
GetAttribute
<div id= "IDQ" iid= "I'm Here" ><span> get text </span></div>
<script type= "Text/javascript" > var d1 = document.getElementById ("IDQ"); Alert (D1.getattribute ("IID"));</script>
③ Removing properties
RemoveAttribute
var D1 = document.getElementById ("IDQ"); D1.removeattribute ("IID");
3. Operation Style
① get Style (only inline styles are available)
JS can only get inline when it gets the style! Others like inline and external need to use jquery.
function Showa () { //1, gets the style var d2 = document.getElementById ("D2"); Alert (D2.style.color) }
② Setting Styles
function Ang () { var d2 = document.getElementById ("D2"); Set style D2.style.backgroundColor = "Red" }
Javascript Base element Operations