Home >Web Front-end >JS Tutorial >A brief discussion on DOM methods in javascript_javascript skills
1: Three ways to obtain objects
1) document.getElementById(id); Get it through the object’s attribute id;
2) element.getElementByTagName(tag); Get by tag name;
3) element.getElementByClassName(class); Get it through the attribute class name;
2: Set and get methods of attributes.
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 properties of 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 possible values in total. But only 3 of them are valuable
nodeType=1 of element node
Attribute node nodeType=2
Text node nodeType=3
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
var p=document.getElementById("p");
p.childNodes[0].nodeValue="Change the content of the text node";
This 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 you all like it.