Introduction to the features (attributes) of the elements obtained and set in javascript learning notes. For details about how to learn javascript, refer to the following section in html:
The Code is as follows:
Baidu
1. Obtain and set the element features through the attributes of the HTMLElement type (object)
The Code is as follows:
Var p = document. getElementById ("myDiv ");
Var img = document. getElementById ("img1 ");
Var a = document. getElementById ("myA ");
// Obtain element features
Alert (p. id); // "myDiv"
Alert (p. className); // "bd", this is not p. class, because class is reserved keyword
Alert (p. title); // "I am p"
Alert (a. href); // http://www.baidu.com
// Set element features
P. id = "myDiv2"; // change the id to "myDiv2"
P. className = "ft"; // change the class to "ft". If there is a style named "ft", it will immediately become "ft" and the browser will immediately respond.
P. title = "myDiv2"; // change the title to "myDiv2"
P. align = "center"; // set center alignment
Img. src = "images/img1.gif"; // sets the image path.
A. innerHTML = "Sina"; // change "Baidu" to "Sina"
A. href = "http://www.sina.com.cn"; // reset the hyperlink
2. the getAttribute (), setAttribute (), and removeAttribute () methods are used to obtain, set, and remove the features of an element (not recommended. IE6 and 7 of the first two methods have an exception, the third method is not supported by IE6 and can be used to set custom features)
The getAttribute () method is used to obtain element features. Take a parameter to obtain the feature name of the element.
SetAttribute () method, used to set element features. Two parameters are used to obtain the feature name and feature value of an element.
The removeAttribute () method is used to remove the features of an element. Accept a parameter, that is, the feature name of the element to be removed.
The Code is as follows:
Var p = document. getElementById ("myDiv ");
Var img = document. getElementById ("img1 ");
Var a = document. getElementById ("myA ");
// Obtain element features
Alert (p. getAttribute ("id"); // "myDiv"
Alert (p. getAttribute ("class"); // "bd", note that this is a class, not a className, which is different from the above
Alert (p. getAttribute ("title"); // "I am p"
Alert (a. getAttribute ("href"); // http://www.baidu.com
// Set element features
P. setAttribute ("id", "myDiv2"); // change the id to "myDiv2"
P. setAttribute ("class", "ft"); // change the class to "ft", which is also a class instead of a className.
P. setAttribute ("title", "myDiv2"); // change the title to "myDiv2"
P. setAttribute ("align", "center"); // sets center alignment.
Img. setAttribute ("src", "images/img1.gif"); // you can specify the image path.
// Remove element features
P. removeAttribute ("class"); // remove the class Feature
3. Get, set, and remove element features through attributes
The Code is as follows:
Var p = document. getElementById ("myDiv ");
// Obtain element features
Alert (p. attributes ["id"]. nodeValue); // "myDiv"
// Set element features
P. attributes ["id"]. nodeValue = "myDiv2"; // change the id to "myDiv2"
// Remove element features
P. attributes. removeNamedItem ("class"); // remove the class Feature