Find length of a visible elements using jQuery

社会主义新天地 提交于 2020-01-02 05:33:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!