javascript jQuery 计算一个类有多少个 div 并放入字符串

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

jQuery count how many divs with a class there are and put into string

javascriptjquery

提问by Jamie Taylor

Is there a way in jQuery to count how many divs you have and put that number into a string

在 jQuery 中有没有办法计算你有多少个 div 并将该数字放入一个字符串中

<div class="name">SOME TEXT</div>

<div class="name">SOME OTHER TEXT</div>

<div class="different">DIFFERENT TEXT</div>

So count the divs with class nameand then put that into a string so the output would be this

所以用class计算div,name然后把它放到一个字符串中,这样输出就是这个

var strNoDivs = 2

Any ideas?

有任何想法吗?

Thanks

谢谢

Jamie

杰米

回答by MatTheCat

var nb = $('div.name').length;

回答by Ferid Gürbüz

First option is:

第一个选项是:

var count= $('div.name').length;

or filter() function can be used too.

也可以使用 filter() 函数。

var count= $('div').filter('.aaa').length;

回答by Matt Ball

var strNoDivs = $('div.name').length;

var strNoDivs = $('div.name').length;

Done.

完毕。

jQuery's selector syntaxis based on the CSS selector syntax (which, I suppose, is only helpful information if you're already familiar with CSS selectors).

jQuery 的选择器语法基于 CSS 选择器语法(如果您已经熟悉 CSS 选择器,我想这只是有用的信息)。

回答by Matt Ball

var strNoDivs = $('div.name').length.toString();

回答by Fermin

var noOfDivs = $('div.name').length?

var noOfDivs = $('div.name').length?

Using the Lengthproperty.

使用Length属性。

回答by Fenton

Like this...

像这样...

var divisions = $("div.name");
var strNoDivs = divisings.length.toString();
alert(strNoDivs);

回答by gdmanandamohon

According to the reference of jQuery Forumit can be done this way.

根据jQuery Forum的参考,它可以通过这种方式完成。

$('input[id*="name"]').length;