createElement() 方法创建新的元素节点:
newElement = xmlDoc.createElement("edition");
xmlDoc.getElementsByTagName("book")[0].appendChild(newElement);
运行实例 »点击 "运行实例" 按钮查看在线实例
xmlDoc 中循环遍历并向所有
for (i = 0; i < xLen; i++) {
newEle = xmlDoc.createElement("edition");
newText = xmlDoc.createTextNode("第一版");
newEle.appendChild(newText);
x[i].appendChild(newEle);
}
运行实例 »点击 "运行实例" 按钮查看在线实例
createAttribute() 用于创建新的属性节点:
newAtt = xmlDoc.createAttribute("edition");
newAtt.nodeValue = "第一版";
xmlDoc.getElementsByTagName("title")[0].setAttributeNode(newAtt);
运行实例 »点击 "运行实例" 按钮查看在线实例
xmlDoc 中"edition""first"循环遍历所有
for (i = 0; i < xLen; i++) {
newAtt = xmlDoc.createAttribute("edition");
newAtt.value = "第一版";
x[i].setAttributeNode(newAtt);
}
运行实例 »点击 "运行实例" 按钮查看在线实例
如果该属性已存在,则将其替换为新属性。
由于 setAttribute() 方法会在属性不存在时创建新属性,因此它也可用于创建新属性。
xmlDoc.getElementsByTagName('book')[0].setAttribute("edition","first");
运行实例 »点击 "运行实例" 按钮查看在线实例
xmlDoc 中"edition" 属性的值设置为 "first"循环遍历所有
for(i = 0; i < x.length; i++) {
x[i].setAttribute("edition", "第一版");
}
运行实例 »点击 "运行实例" 按钮查看在线实例
createTextNode() 方法创建新的文本节点:
newEle = xmlDoc.createElement("edition");
newText = xmlDoc.createTextNode("first");
newEle.appendChild(newText);
xmlDoc.getElementsByTagName("book")[0].appendChild(newEle);
运行实例 »点击 "运行实例" 按钮查看在线实例
xmlDoc 中"first"将带有文本节点的元素节点添加到所有
for (i = 0; i < xLen; i++) {
newEle = xmlDoc.createElement("edition");
newText = xmlDoc.createTextNode("第一版");
newEle.appendChild(newText);
x[i].appendChild(newEle);
}
运行实例 »点击 "运行实例" 按钮查看在线实例
createCDATASection() 方法创建新的 CDATA section 节点。
newCDATA = xmlDoc.createCDATASection("新年特惠 & 限时折扣");
xmlDoc.getElementsByTagName("book")[0].appendChild(newCDATA);
运行实例 »点击 "运行实例" 按钮查看在线实例
xmlDoc 中循环遍历并向所有
x = xmlDoc.getElementsByTagName("book");
xLen = x.length;
newtext = "新年特惠 & 限时折扣";
for (i = 0; i < xLen; i++) {
newCDATA = xmlDoc.createCDATASection(newtext);
x[i].appendChild(newCDATA);
}
运行实例 »点击 "运行实例" 按钮查看在线实例
createComment() 方法创建新的注释节点。
newComment = xmlDoc.createComment("2024 年 2 月修订");
xmlDoc.getElementsByTagName("book")[0].appendChild(newComment);
运行实例 »点击 "运行实例" 按钮查看在线实例
xmlDoc 中循环遍历并向所有
x = xmlDoc.getElementsByTagName("book");
xLen = x.length
for (i = 0; i < xLen; i++) {
newComment = xmlDoc.createComment("2024 年 2 月修订");
x[i].appendChild(newComment);
}
运行实例 »点击 "运行实例" 按钮查看在线实例
相关
视频
RELATED VIDEOS
科技资讯
1
2
3
4
5
6
7
8
9
精选课程
共5课时
17.2万人学习
共49课时
77万人学习
共29课时
61.7万人学习
共25课时
39.3万人学习
共43课时
71万人学习
共25课时
61.6万人学习
共22课时
23万人学习
共28课时
33.9万人学习
共89课时
125万人学习