Home >Web Front-end >Front-end Q&A >What are the commonly used methods of DOM in JavaScript?
Commonly used methods: appendChild(), insertBefore(), hasChildNodes(), removeChild(), replaceChild(), cloneNode(), write(), open(), writeln(), etc.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Member methods of nodes
appendChild(): Add a child node at the end of the node’s child node list:
var method = document.getElementById('method'); var input = document.createElement('input'); method.appendChild('input');
insertBefore(): Add a child node before the specified node in the node's child node list:
method.insertBefore(input,method.childNodes[1]);
hasChildNodes(): Check whether a node has child nodes:
method.hasChildNodes();
removeChild(): Remove the specified node of the node:
method.removeChild(method.childNodes[1]);
replaceChild(): Use the specified node to replace another specified child node:
method.replaceChild(input,method.childNodes[1]);
cloneNode() Clone node :
var relation=document.getElementById('relation'); var newRel=relation.cloneNode(true);
document document node
Method to get element node
getElementById(): Get element node by ID
var ele=document.getElementById('ele');
getElementsByTagName(): Get the node list collection through the tag name
var ps=document.getElementsByTagName('p'); console.log(ps.length);"
getElementsByName(): Get the element node collection through the Name attribute:
var sexs=document.getElementsByName('user'); console.log(sexs);"
Create Node methods
createElement(): Create an element node
var b=document.createElement('b');
createAttribute(): Create an attribute node
var classAttr=document.createAttribute('class'); classAttr.value='on';"
createTextNode(): Create a text node
var newtext=document.createTextNode('First');
Document stream operation
write(): Input text stream to the page
document.write('哈哈');
writeln(): Enter a text stream into the page and add\n
document.writeln('哈哈');
open(): Open a document stream
document.open();
close(): Close a document stream
document.close();
[Recommended learning: javascript advanced tutorial】
The above is the detailed content of What are the commonly used methods of DOM in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!