获取项目数jQuery.each()

har*_*anb 6 javascript xml jquery xml-parsing

我正在使用jQuery解析XML.我想得到具有给定标签名称的所有子节点的计数.

例如:

<People>
<person name="hello'></person>
<person name="hello'></person>
<person name="hello'></person>
<person name="hello'></person>
<person name="hello'></person>
</people>
Run Code Online (Sandbox Code Playgroud)

我使用以下jQuery代码:

$(xml).find("person").each(function(){});
Run Code Online (Sandbox Code Playgroud)

当然上面的代码有效,但我只想得到计数,我不想循环.原因是:上面的示例太简单了,我的XML文件和javascript代码有点复杂,因此有很多逻辑可以找出xml文件,而且我不想用代码编写所有这些.

非常感谢!

Jar*_*Par 8

如果您想获得计数,请使用该length属性:

$(xml).find("person").length;
Run Code Online (Sandbox Code Playgroud)