在jQuery中计算和显示元素的数量

时间:2013-12-27 09:30:13

标签: javascript jquery html

我想计算并显示所有项目的span class =“check_box cb”(完整)和span class =“uncheck_box cb(不完整)旁边的fiddle demo

HTML:

<span><a href="javascript:;" class="sort active showall">All</a></span>
            <span><a href="javascript:;" class="sort complete">Completed</a></span>
            <span><a href="javascript:;" class="sort incomplete">Incomplete</a></span>

<ul class="leftlist">
<li class="todo" id="1011" itemage="1"><a href="javascript:;"> 
    <a href="javascript:;" class="strike">           
                    <span class="check_box cb"></span>

                <p>Option 1 Complete</p> </a>
</li>

<li class="todo" id="1011" itemage="1"><a href="javascript:;"> 
    <a href="javascript:;">           
                    <span class="uncheck_box cb"></span>

                <p>Option 1 Incomplete</p> </a>
</li>

<li class="todo" id="1011" itemage="1"><a href="javascript:;"> 
    <a href="javascript:;" class="strike">           
                    <span class="check_box cb"></span>

                <p>Option 1 Complete</p> </a>
</li>

<li class="todo" id="1011" itemage="2"><a href="javascript:;"> 
    <a href="javascript:;">          
                    <span class="uncheck_box cb"></span>

                <p>Option 2 InComplete</p> </a>
</li>

1 个答案:

答案 0 :(得分:0)

Jsfiddle:http://jsfiddle.net/patelmilanb1/6CNA2/2/

<div class="complete-count">Complete Count: </div>
<div class="incomplete-count">Incomplete Count:</div>

$('.complete-count').append($('.check_box').length);
$('.incomplete-count').append($('.uncheck_box').length);

您可以使用$(element).length

获取DOM中任何元素的长度

建议:确保为完整.comple和不完整.incomple等元素提供差异类,您所做的是正确的,但您的课程具有误导性。