Home >Web Front-end >JS Tutorial >Use js to complete operations such as adding, deleting, modifying, and copying nodes_javascript skills
Requirements: Complete the operation of adding, deleting, modifying and copying nodes
Methods and attributes used:
1. Get the parent node of a certain node
parentNode attribute
2. Get the parent node of a certain node Child node collection
childNodes attribute
3. Create a new node
createTextNode (node text content) The method of the document object is not very compatible with some browsers
createElement (object) Methods of the document object, for example: document.createElement("a");
4. Add attributes and attribute values to a node object
setAttribute(attribute, attribute value); For example: aNode.setAttribute("href" ,"http://www.baidu.com");
5. Replace the child node under a certain node
replaceChild (new node, atomic node);
6. Add a node to Under a node
appendChild (node to be added)
7. Clone a node
cloneNode(). Passing no parameters is the same as passing the true parameter, which means cloning the node includes child nodes