如何计算同一类的元素数量? [英] How can I count the number of elements with same class?
本文介绍了如何计算同一类的元素数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的页面上有一个主要的div,带有一个特定的id
.现在,此div
中存在一些相同类的输入元素.那么如何使用 jQuery 在此div
中计算相同类的这些元素的数量?
I have a main div in my page with a specific id
. Now some input elements of the 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;
这篇关于如何计算同一类的元素数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文