我正在尝试获取XML节点的子节点长度。其他浏览器似乎给出了除chrome之外的确切结果。假设如果子项数为6,则chrome返回13.因此,方法hasChildNodes()
或hasChildren()
会返回Uncaught TypeError: Cannot call method 'hasChildNodes' of undefined
错误。您能否请一些替代方案,以便我可以在Chrome中运行我的代码。
$.ajax({
type: "GET",
url: "tree1.xml",
dataType:"xml",
success: function(xml){
var root=xml.documentElement;
var childs1=root.childNodes;
var len=$(childs1).size();
alert(len);
这是代码的一部分,我在警告孩子的数量。根元素有6个孩子。但是铬警报13
答案 0 :(得分:0)
您想要的是.children
而不是.childNodes
。
后者也包括文本节点,因此通常会返回更高的数字;更糟糕的是,他们不支持与元素相同的方法。
要确定子元素的数量,可以使用.length
属性而不是将其包装在jQuery中。
var childs1 = root.children;
var len = childs1.length;