admin管理员组文章数量:1128014
What I'm trying to do is to count all of the elements in the current page with the same class and then I'm going to use it to be added onto a name for an input form. Basically I'm allowing users to click on a <span>
and then by doing so add another one for more of the same type of items. But I can't think of a way to count all of these simply with jQuery/JavaScript.
I was going to then name the item as something like name="whatever(total+1)"
, if anyone has a simple way to do this I'd be extremely grateful as JavaScript isn't exactly my native tongue.
What I'm trying to do is to count all of the elements in the current page with the same class and then I'm going to use it to be added onto a name for an input form. Basically I'm allowing users to click on a <span>
and then by doing so add another one for more of the same type of items. But I can't think of a way to count all of these simply with jQuery/JavaScript.
I was going to then name the item as something like name="whatever(total+1)"
, if anyone has a simple way to do this I'd be extremely grateful as JavaScript isn't exactly my native tongue.
- 3 the first person i saw got it and i didn't know it was a simple $('.classname').length to get it. If i had more to give i would. Didn't think to try it like that. I've been setting variables quite a bit and simple things like appending documents etc. etc. Wow i feel pretty dumb now i should've thought to look for the .length() command to see what it said... thanks to everyone btw. – 133794m3r Commented Apr 28, 2010 at 7:42
- 29 +1 For asking a question like Yoda – jbnunn Commented Mar 7, 2012 at 17:14
- 7 @jbnunn what is Yoda? – shasi kanth Commented Oct 30, 2014 at 6:18
6 Answers
Reset to default 815Should just be something like:
// Gets the number of elements with class yourClass
var numItems = $('.yourclass').length
As a side-note, it is often beneficial to check the length property before chaining a lot of functions calls on a jQuery object, to ensure that we actually have some work to perform. See below:
var $items = $('.myclass');
// Ensure we have at least one element in $items before setting up animations
// and other resource intensive tasks.
if($items.length)
{
$items.animate(/* */)
// It might also be appropriate to check that we have 2 or more
// elements returned by the filter-call before animating this subset of
// items.
.filter(':odd')
.animate(/* */)
.end()
.promise()
.then(function () {
$items.addClass('all-done');
});
}
Getting a count of the number of elements that refer to the same class is as simple as this
<html>
<head>
<script src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
alert( $(".red").length );
});
</script>
</head>
<body>
<p class="red">Test</p>
<p class="red">Test</p>
<p class="red anotherclass">Test</p>
<p class="red">Test</p>
<p class="red">Test</p>
<p class="red anotherclass">Test</p>
</body>
</html>
var count = $('.' + myclassname).length;
HTML:
<div>
<img src='' class='class' />
<img src='' class='class' />
<img src='' class='class' />
</div>
JavaScript:
var numItems = $('.class').length;
alert(numItems);
Fiddle demo for inside only div
for counting:
$('.yourClass').length;
should work fine.
storing in a variable is as easy as:
var count = $('.yourClass').length;
try
document.getElementsByClassName('myclass').length
let num = document.getElementsByClassName('myclass').length;
console.log('Total "myclass" elements: '+num);
.myclass { color: red }
<span class="myclass" >1</span>
<span>2</span>
<span class="myclass">3</span>
<span class="myclass">4</span>
本文标签: javascriptjQuery counting elements by classwhat is the best way to implement thisStack Overflow
版权声明:本文标题:javascript - jQuery counting elements by class - what is the best way to implement this? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736708695a1948819.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论