计算div中相同类的元素数量? [英] count the number of element of same class in a div?
本文介绍了计算div中相同类的元素数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的页面中有一个带有特定ID的主div。现在这个div中存在同一类的一些输入元素。那么如何使用jquery计算这个div中相同类的这些元素的数量?
I have a main div in my page with a specific id. Now some input elements of same class are present in this div. so how can I count the number of these elements of same class in this div using jquery?
推荐答案
使用jQuery你可以简单地使用
With jQuery you can simply use
$('#main-div .specific-class').length
否则使用普通js(包括IE8)你可以简单地写
otherwise with plain js (from IE8 included) you could simply write
document.querySelectorAll('#main-div .specific-class').length;
这篇关于计算div中相同类的元素数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文