The removeChild() method removes a specified node.
The removeAttribute() method removes a specified attribute.
The removeChild() method removes a specified node.
When a node is removed, all its child nodes are also removed.
This code will remove the first <book> element from the loaded xml:
y = xmlDoc.getElementsByTagName("book")[0];
xmlDoc.documentElement.removeChild(y);
Try it Yourself »
Example explained:
The removeChild() method is the only way to remove a specified node.
When you have navigated to the node you want to remove, it is possible to remove that node using the parentNode property and the removeChild() method:
Example explained:
The removeChild() method can also be used to remove a text node:
x = xmlDoc.getElementsByTagName("title")[0];
y = x.childNodes[0];
x.removeChild(y);
Try it Yourself »
Example explained:
It is not very common to use removeChild() just to remove the text from a node. The nodeValue property can be used instead. See next paragraph.
The nodeValue property can be used to change the value of a text node:
Example explained:
The removeAttribute() method removes an attribute node by its name.
Example: removeAttribute('category')
This code removes the "category" attribute in the first <book> element:
x = xmlDoc.getElementsByTagName("book");
x[0].removeAttribute("category");
Try it Yourself »
Example explained:
Loop through and remove the "category" attribute of all <book> elements: Try it yourself
The removeAttributeNode() method removes an attribute node, using the node object as parameter.
Example: removeAttributeNode(x)
This code removes all the attributes of all <book> elements:
x = xmlDoc.getElementsByTagName("book");
for (i = 0; i < x.length; i++) {
while (x[i].attributes.length > 0) {
attnode = x[i].attributes[0];
old_att = x[i].removeAttributeNode(attnode);
}
}
Try it Yourself »
Example explained:
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!