网络学院 w3pop社区 网络资源 IT新闻

w3pop.com :: 网络学院 :: XML DOM :: DOM 克隆节点

会员登陆

帐号

密码

回答

记住密码

忘记密码? 注册

XML DOM
DOM 节点
DOM 节点列表
DOM 解析
DOM 遍历节点树
DOM Mozilla 和 I..
DOM 获取节点
DOM 设置节点
DOM 删除节点
DOM 更换节点
DOM 建立节点
DOM 添加节点
DOM 克隆节点
DOM 节点类型
DOM Node
DOM NodeList
DOM NamedNodeMap
DOM Document
DOM DocumentType
DOM ProcessingIn..
DOM Element

DOM 克隆节点


作者:w3pop.com 翻译/整理:w3pop.com 发布:2007-04-29 修改:2007-09-18 浏览:4224 :: ::

Examples
案例

In the examples below, we will use the XML file books.xml, and the JavaScript function loadXMLDoc().
在下述案例中,我们会使用到下面这两个XML文件:books.xml和Java脚本函数 loadXMLDoc()

 复制一个节点并将它添加到节点列表中
This example uses cloneNode() to copy a node and append it to a node list.
这个案例使用了cloneNode()方法复制了一个节点并将它添加到了一个节点列表中。


Copy a Node
复制一个节点

The cloneNode() method creates a copy of a specified node.
cloneNode()方法的作用是创建一个指定节点的复制版本。

The cloneNode() method has a parameter (true or false). This parameter indicates if the cloned node should include all attributes and child nodes of the original node.
cloneNode()方法拥有一个参数(true或false)。这个参数指明了被克隆的节点是否应该包含所有的属性以及原始节点中的子节点。

The following code fragment copies the first <book> node and then adds the copy to the end of the node list:
下述代码片断复制了第一个<book>节点并将它添加到节点列表的末尾:

xmlDoc=loadXMLDoc("books.xml");
var oldNode=xmlDoc.getElementsByTagName('book')[0];
var newNode=oldNode.cloneNode(true);
xmlDoc.documentElement.appendChild(newNode);

//Output all titles
var y=xmlDoc.getElementsByTagName("title");
for (i=0;i<y.length;i++)
{
document.write(y[i].childNodes[0].nodeValue);
document.write("<br />");
}

Output:
输出结果:

Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML
Everyday Italian

评论 (0) All