问题
Hi all I need to find the length of all the li elements which has display block only. How can this be possible using jQuery. I have a category menu block which has more link at the bottom which when clicked will displays the all categories.The Bottom link now turn to Less which when clicked displays less items. Here is the code.
var list = $('.menu-categories-list ul li:gt(3)');
list.hide();
$('#ClickMore').click(function() {
list.slideToggle(400);
if( $(this).parent().prev().children().length < 1 ) {
$(this).html('Less...');
}
else {
$(this).html('More...');
}
return false;
});
YOu can have a look at the link. The categories block on the left side
回答1:
I would suggest:
$('.menu-categories-list ul li:visible').size()
in condition:
if ( $('.menu-categories-list ul li:visible').size() >= 4 ) {
// do something
}
回答2:
For anyone coming from Google...
The .size()
method has been deprecated as of jQuery 1.8, use .length
instead:
$('.menu-categories-list ul li:visible').length
Will return an integer value depicting the number of visible li
elements that are children of .menu-categories > ul
Written as an answer as I don't have enough rep to comment on Teneff's answer
来源:https://stackoverflow.com/questions/7334573/find-length-of-a-visible-elements-using-jquery