在 Javascript 中克隆 XML 文档的最佳方法是什么?
我试过
var newDocument = myDocument.cloneNode(true);
但那只是返回了 null。我也考虑过做
var newNode = myDocument.documentElement.cloneNode(true);
但这对我的目的来说还不够,因为这样新节点就有了和以前一样的 ownerDocument
。
最佳答案
您可以执行类似以下操作来克隆 XML 文档:
var newDocument = oldDocument.implementation.createDocument(
oldDocument.namespaceURI, //namespace to use
null, //name of the root element (or for empty document)
null //doctype (null for XML)
);
var newNode = newDocument.importNode(
oldDocument.documentElement, //node to import
true //clone its descendants
);
newDocument.appendChild(newNode);
关于javascript - 如何在 Javascript 中克隆 XML 文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9711046/