我不确定是我现有代码中存在错误,还是Jquery数组的功能如下:
var categories = [];
$(this).children('categories').each(function() {
categories.push($(this).find('name').text());
});
现在,当我有以下的XML节点时:
<categories>
<name>a</name>
<name>b</name>
<name>c</name>
</categories>
我看到在Firebug中,categories数组只有一个元素 - “abc”,但实际上它应该作为索引2,并且值为'a','b'和'c'。
我的代码有什么问题吗?
.text()
方法将连接值。 - Philfind('categories name')
,因为我不想在树节点中获取所有内容(类别节点在 XML 树中重复)。所以我只能使用.children
。 - Programmerchildren('categories').find('name')
,不要像您选择的答案那样使用两个each
循环。 - James Montagne