DOM Node对象appendChild(newChild)方法在指定元素节点的最后一个子节点之后添加一个节点。 它返回添加新添加的节点。
语法
以下是appendChild属性的使用语法。
nodeObject.appendChild(newChild)
参数
newChild - 它是要添加/追加的新节点,它是Node类型。
返回值
此方法返回添加的节点。
示例
文件:node.xml 的内容如下 -
Susen
Su
1584567890
[email protected]
Max
Su
1334667898
[email protected]
Min
Su
1364562350
[email protected]
以下示例演示了appendChild属性的用法,文件:appendchild.html -
function loadXMLDoc(filename) {
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else{ // code for IE5 and IE6
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseXML;
}
xmlDoc = loadXMLDoc("/node.xml");
create_e = xmlDoc.createElement("PhoneNo");
x = xmlDoc.getElementsByTagName("FirstName")[0];
x.appendChild(create_e);
document.write("新增加的子元素是: ")
document.write(x.getElementsByTagName("PhoneNo")[0].nodeName);
执行上面代码,得到以下结果 -
¥ 我要打赏
纠错/补充
收藏
加QQ群啦,易百教程官方技术学习群
注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。