Home > Article > Web Front-end > A brief discussion on DOM methods in javascript
1: Three ways to get the object
1) document.getElementById(id); Get it through the object’s attribute id;
2)element.getElementByTagName(tag); Get it by the tag name;
3)element.getElementByClassName(class);Get it by the attribute class name;
2:The set and get methods of the attribute.
1) object.getAttribute(attribute); Get the attribute of the element. ===>>>HTMLDOM: object.attribute;
2) object.setAttribute(attribute,value);Set the attribute of the element.= ===>>>HTMLDOM:object.attribute=value;
3: Some attributes about nodes
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 possibilities in total The value. But only 3 of them have use value
The nodeType of the element node=1
The nodeType=2
of the attribute node The nodeType=3# of the text node
## 3) nodeValue attribute: The main function is to change the text content in the element. The function is similar to object.innerHTML For example:This is a text node p>
var p=document.getElementById("p"); p.childNodes[0].nodeValue="Change the content of the text node"; like this It changes the content in the p node; It can also be implemented like this: p.innerHTML="Change the content of the text node";The above is the entire content of this article, I hope everyone To be able to like. For more articles related to the DOM method in JavaScript, please pay attention to the PHP Chinese website!