javascript - 使用javascript将所有类型1的xml节点插入到变量中

标签 javascript xml

我有以下内容:

root = xmlDoc.documentElement;

for (i=0; i<root.childNodes.length; i++) {              

if (root.childNodes[i].nodeType==1){ // FILTERING NODE TYPE
myvar = root.childNodes.length;
alert (myvar.length);
}
}

长度返回 7 - 反射(reflect)所有节点类型(未过滤)。

我想要的是:myvar 应该只包含类型 1 的节点元素

我怎样才能做到这一点?

谢谢, L.

最佳答案

使用parseFloat函数获取int值进行比较

root = xmlDoc.documentElement;

for (i=0; i<root.childNodes.length; i++) {              

  if (parseFloat(root.childNodes[i].nodeType)==1){ // FILTERING NODE TYPE
     myvar = root.childNodes.length;
     alert (myvar.length);
  }
}

关于javascript - 使用javascript将所有类型1的xml节点插入到变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12565899/

相关文章:

javascript - 使用 jquery 滚动到页面上的特定元素

javascript - location.href 仅适用于 Chrome

c# - 如何在 C# 中使用 XMLREADER 从 XML 字符串中读取特定元素

javascript - 使用 Javascript 遍历 Xml

javascript - 将 "simple"时区偏移量 (-7) 转换为 "full"偏移量 (-07 :00)

javascript - 如何动态设置ng-model

html - XPath//*[text()]没有选择我想要的文本

xml - 将 xslt 修改为与命名空间无关

c++ - 与 replaceChild 相关的内存泄漏

html - xsl 为每个节点创建折叠/展开面板