使用Javascript复制DOM中的节点

Copy nodes in DOM using Javascript

本文关键字:节点 DOM Javascript 复制 使用      更新时间:2023-09-26

我有一个这样的XML文件:

<data>
 <First id="FirstOne">
  <lines id="Lines">
   <Second id= "second" ColorPerVertex='true'></Second>
   <third id="third" color ='true'></third>
  </lines>
 </First>
</data>

我试图复制parentnode行的所有子节点。

这是我尝试过的片段:

var txt=$(xml).find("lines").children();
alert(txt); // to check if it is really does fetch it. 

它不输出包括其属性在内的所有子级。

我试着在这里研究这个例子。即使这样对我也不起作用。

有什么办法可以让我做这件事吗?

来自文档:


jQuery(html[,ownerDocument])

html: A string of HTML to create on the fly. Note that this parses HTML, not XML.


请改用$.parseXML来创建XML文档。

var txt=$($.parseXML(xml)).find("lines").children();