【问题标题】:Copy nodes in DOM using Javascript使用 Javascript 在 DOM 中复制节点
【发布时间】:2012-04-01 20:15:25
【问题描述】:

我有一个这种类型的 XML 文件:

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

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

这是我尝试过的那种sn-p:

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

它不会输出所有的孩子,包括它的属性。

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

有什么方法可以做到吗?

【问题讨论】:

  • 尝试console.log(txt)而不是警报然后检查控制台
  • @Joseph :它会根据需要在控制台中获取它。但它为什么不提醒?
  • 警报不提供详细信息。当您传递文本时,它只是“详细”。对于对象,它是无用的。不要使用警报进行调试。

标签: javascript jquery xml


【解决方案1】:

来自docs


jQuery(html [, ownerDocument])

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


使用$.parseXML 创建 XML 文档。

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

【讨论】:

  • 你能解释一下这对我有什么帮助吗?
  • 对不起,我的错。看看 var txt=$($.parseXML(xml)).find("lines").children();
猜你喜欢
  • 2010-10-31
  • 2021-01-22
  • 2011-01-19
  • 1970-01-01
  • 2017-01-19
  • 1970-01-01
  • 1970-01-01
  • 2013-03-02
  • 1970-01-01
相关资源
最近更新 更多