获取项目的数量。每个()

时间:2021-07-17 12:07:21

I am parsing XML using jQuery. I want to get the count of all the sub nodes with the given tag name.

我正在使用jQuery解析XML。我想获取所有具有给定标记名的子节点的计数。

For example:

例如:

<People>
<person name="hello'></person>
<person name="hello'></person>
<person name="hello'></person>
<person name="hello'></person>
<person name="hello'></person>
</people>

I use the following jQuery Code:

我使用以下jQuery代码:

$(xml).find("person").each(function(){});

Of course the above code works, but I just want to get the count, I do not want to loop. The reason is this: The above sample is too easy, my XML file and javascript code is a bit complex, so there is a lot of logic to figure out the xml file, and I don't want to spend code writing all that.

当然,上面的代码可以工作,但是我只想获得计数,我不想循环。原因是:上面的示例太简单了,我的XML文件和javascript代码有点复杂,所以要计算XML文件有很多逻辑,我不想用代码来编写所有这些代码。

Many Thanks!

很多谢谢!

2 个解决方案

#1


8  

If you want to get the count then use the length property:

如果你想要得到计数,那么使用长度属性:

$(xml).find("person").length;

#2


1  

Or also try size():

也或者尝试大小():

$(xml).find("person").size();

#1


8  

If you want to get the count then use the length property:

如果你想要得到计数,那么使用长度属性:

$(xml).find("person").length;

#2


1  

Or also try size():

也或者尝试大小():

$(xml).find("person").size();