DOM is the model of Html pages. Every tag is used as an object. by calling the attributes and methods in DOM, JavaScript can program and control the text box, layer, and other elements in the webpage. For example, you can read the value in the text box and set the value in the text box by operating the DOM object of the text box. 1. Three Methods for getting objects
1) document. getElementById (id); obtained through the object's property id;
2) element. getElementByTagName (tag); obtained by tag name;
3) element. getElementByClassName (class); obtained by the attribute class name;
2: set and get methods of the attribute.
1) object. getAttribute (attribute); get the attributes of an element. ==>>> HTMLDOM: object. attribute;
2) object. setAttribute (attribute, value); set the attribute of the element. ====>>> HTMLDOM: object. attribute = value;
3: Some node attributes
1) childNodes attribute: returns an array.
Element. childNodes [0] is equivalent to element. firstChild.
Element. childNodes [element. childNodes. length] is equivalent to element. lastChild.
2) nodeType attribute: there are 12 possible values, but only 3 of them are useful.
Element Node nodeType = 1
Attribute node nodeType = 2
Text node nodeType = 3
3) nodeValue attribute: The main function is to change the text content of an element. The function is similar to object. innerHTML.
For example:
This is a text node
Var p = document. getElementById ("p ");
P. childNodes [0]. nodeValue = "change the content of a text node ";
In this way, the content in the p node is changed;
You can also achieve this: p. innerHTML = "change the content of a text node ";
The above is all the content of this article. I hope you will like it.
For more information about DOM methods in javascript, please follow the PHP Chinese website!