Home > Web Front-end > Front-end Q&A > What are the commonly used methods of DOM in JavaScript?

What are the commonly used methods of DOM in JavaScript?

青灯夜游
Release: 2023-01-07 11:43:58
Original
2236 people have browsed it

Commonly used methods: appendChild(), insertBefore(), hasChildNodes(), removeChild(), replaceChild(), cloneNode(), write(), open(), writeln(), etc.

What are the commonly used methods of DOM in JavaScript?

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');
Copy after login

insertBefore(): Add a child node before the specified node in the node's child node list:

method.insertBefore(input,method.childNodes[1]);
Copy after login

hasChildNodes(): Check whether a node has child nodes:

method.hasChildNodes();
Copy after login

removeChild(): Remove the specified node of the node:

method.removeChild(method.childNodes[1]);
Copy after login

replaceChild(): Use the specified node to replace another specified child node:

method.replaceChild(input,method.childNodes[1]);
Copy after login

cloneNode() Clone node :

var relation=document.getElementById('relation');        
var newRel=relation.cloneNode(true);
Copy after login

document document node

Method to get element node

getElementById(): Get element node by ID

var ele=document.getElementById('ele');
Copy after login

getElementsByTagName(): Get the node list collection through the tag name

var ps=document.getElementsByTagName('p');           
console.log(ps.length);"
Copy after login

getElementsByName(): Get the element node collection through the Name attribute:

var sexs=document.getElementsByName('user');           
console.log(sexs);"
Copy after login

Create Node methods

createElement(): Create an element node

var b=document.createElement('b');
Copy after login

createAttribute(): Create an attribute node

var classAttr=document.createAttribute('class');
classAttr.value='on';"
Copy after login

createTextNode(): Create a text node

var newtext=document.createTextNode('First');
Copy after login

Document stream operation

write(): Input text stream to the page

document.write('哈哈');
Copy after login

writeln(): Enter a text stream into the page and add\n

document.writeln('哈哈');
Copy after login

open(): Open a document stream

document.open();
Copy after login

close(): Close a document stream

document.close();
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template