计算jQuery可见的li元素 [英] Count li elements that are visible with jQuery

查看:109
本文介绍了计算jQuery可见的li元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下jQuery脚本计数li元素:

Im counting my li elements with the following jQuery script:

HTML:

<ul class="relatedelements">
   <li style="display:none;" class="1">anything</li>
   <li style="display:none;" class="2">anything</li>
   <li style="display:none;" class="3">anything</li>
</ul>

jQuery:

    $(function() {
        var numrelated=$('.relatedelements > li').length;
        $('.num-relatedelements').html(numrelated); 
    });

->脚本返回: 3

使用jQuery $(document).ready时,我更改了某些li元素的style="display: none"属性,例如:$('.2').show();

I change the style="display: none" property of some of the li elements when $(document).ready with jQuery, like: $('.2').show();

我现在想通过以下脚本来更改脚本,使其仅计算可见的li元素(我刚刚在jQuery docs之后添加了:visible):

I now want to change the script in a way to count only the visible li elements with the following script (i just added :visible following the jQuery docs):

    $(function() {
        var numrelated=$('.relatedelements > li:visible').length;
        $('.num-relatedelements').html(numrelated); 
    });

->脚本返回:什么

我不知道为什么它行不通-也许有人有任何建议或想法? 任何帮助都非常感激.预先感谢!

I have no clue why it doesn't work out - maybe anyone has any tip or idea? Any help is much appreaciated. Thanks upfront!

推荐答案

对我来说很好

$(document).ready(function(){
    $('.2').show();
    var numrelated=$('.relatedelements > li:visible').length;
    $('.num-relatedelements').html(numrelated); 
});​

JsFiddle Lind: http://jsfiddle.net/xuckF/1/

JsFiddle Lind : http://jsfiddle.net/xuckF/1/

这篇关于计算jQuery可见的li元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆