Count elements with jQuery
Is there a way to co开发者_JAVA技巧unt how many elements on the page with a particular class?
$('.someclass').length
You could also use:
$('.someclass').size()
which is functionally equivalent, but the former is preferred. In fact, the latter is now deprecated and shouldn't be used in any new development.
var count_elements = $('.class').length;
From: http://api.jquery.com/size/
The .size() method is functionally equivalent to the .length property; however, the .length property is preferred because it does not have the overhead of a function call.
Please see:
http://api.jquery.com/size/
http://api.jquery.com/length/
Yes, there is.
$('.MyClass').size()
I believe this works:
$(".MyClass").length
try this:
var count_element = $('.element').length
$('.class').length
This one does not work for me. I'd rather use this:
$('.class').children().length
I don't really know the reason why, but the second one works only for me. Somewhy, either size doesn't work.
The best way would be to use .each()
var num = 0;
$('.className').each(function(){
num++;
});
use the .size()
method or .length
attribute
精彩评论