如果元素可见,则自动增加步骤编号

时间:2014-06-19 22:09:11

标签: javascript jquery html

尝试自动增加表单的步骤编号。表单的某些元素可能隐藏或显示依赖于上面的答案。
HTML

<div class="confirmation-step">
    <span class="step-number"></span>
</div>
<div class="confirmation-step" style="display:none;">
    <span class="step-number"></span>
</div>
<div class="confirmation-step">
    <span class="step-number"></span>
</div>

的jQuery

$('.confirmation-step').each(function() {
    if($(this).is(':visible')) {
        //set variable stepNum
        //Haven't come up with anything good yet
    } else {}
    $(this).find('.step-number').html(stepNum);
});

样本输出

<div class="confirmation-step">
    <span class="step-number">1</span>
</div>
<div class="confirmation-step" style="display:none;">
    <span class="step-number"></span>
</div>
<div class="confirmation-step">
    <span class="step-number">2</span>
</div>

谢谢!

1 个答案:

答案 0 :(得分:1)

怎么样

$('.confirmation-step:visible .step-number').text(function(i) {return i+1;});

FIDDLE