jQuery 如何使用jQuery查找元素的数量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7632855/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:05:40  来源:igfitidea点击:

How to find the count of elements using jQuery

jquery

提问by yrksundeep

I have a span tag and that holds some n number of span tags.. can I get the number of span tags available with in the parent span using Jquery.

我有一个 span 标签,它包含一些 n 个 span 标签。我可以使用 Jquery 获取父 span 中可用的 span 标签的数量吗?

example:

例子:

<span class="x">
    <span id="1">one</span>
    <span id="2">two</span>
    <span id="3">three</span>
</span>

now i should find the count of number of span tags inside the parent span and my output is 3 according to the above scenario.

现在我应该找到父跨度内跨度标签的数量,根据上述情况,我的输出为 3。

please help me..

请帮我..

回答by Andy

my version :)

我的版本:)

$('span.x > span').length

回答by Shawn Chin

$("span.x").children("span").size()

Demo: http://jsfiddle.net/AZXv3/

演示:http: //jsfiddle.net/AZXv3/

回答by leonm

From the documentationyou can use length or size(). You can chain jquery selectors with findto count the inner spans. For example,

文档中,您可以使用 length 或 size()。您可以使用find链接 jquery 选择器来计算内部跨度。例如,

<script>
  var n = $(".x").find("span").length;
  alert(n);
</script>

回答by Siten

Try:

尝试:

$("#id od 1st span").children("span").size();

I think this will help you.

我认为这会对你有所帮助。

回答by Frederiek

$('.x span').length

this should work

这应该有效

http://jsfiddle.net/6kWdf/

http://jsfiddle.net/6kWdf/

回答by Gokul Patil

You could use this for example:

例如,您可以使用它:

$('#showTotalBarcodeCnt').html($("#barcodeLabelList").find("label").length);

Thanks

谢谢