javascript 获取项目数 jQuery.each()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9473590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get count of items jQuery.each()
提问by harsimranb
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!
非常感谢!
回答by JaredPar
If you want to get the count then use the length
property:
如果要获取计数,请使用以下length
属性:
$(xml).find("person").length;
- API Reference: http://api.jquery.com/length/
- API参考:http: //api.jquery.com/length/
回答by ZloyPotroh
Or also try size()
:
或者也尝试size()
:
$(xml).find("person").size();