简体   繁体   English

jQuery每次单击事件一一显示div

[英]jquery to show div one by one each click event

This is my code : 这是我的代码:

   <script type="text/javascript">
    $(document).ready(function(){
        $('.toAdd').hide();
        $('#add').click(function () {
            var i = 0;
            $('.toAdd').each(function () {
                if ($(this).show()) {
                    i++;
                }
            });
        });
    });
    </script>

    <div id=add><input type="button" value="click"/>
    </div>
    <div id="toAdd">
    <label>1</label>
    </div>
    <div id="toAdd">
    <label>2</label>
    </div>
    <div id="toAdd">
    <label>3</label>
    </div>
    <div id="toAdd">
    <label>4</label>
    </div>

In this code i need to show div one by one for each click event but its not working? 在此代码中,我需要为每个点击事件一个一个显示div,但它不起作用?

ID must be unique , use classes instead. ID必须是唯一的 ,请改用

$('.toAdd').hide();

var count = 0;
$('input').on('click',function(){
    $('.toAdd:eq('+count+')').show();
    count++;
});

JSFiddle JSFiddle

在您的div中,将id =“ toAdd”更改为class =“ toAdd”

As others have said, you need to change id="toAdd" to `class="toAdd". 正如其他人所说,您需要将id="toAdd"更改为`class =“ toAdd”。

You can then show the first unhidden DIV with: 然后,您可以使用以下命令显示第一个未隐藏的DIV:

    $('#add input').click(function () {
        $('.toAdd:hidden:first').show();
    });

FIDDLE 小提琴

Replace <div id="toAdd"> with <div class="toAdd"> <div id="toAdd"> with <div class="toAdd">

and do this 并做到这一点

  $('.toAdd').each(function () {
                if (!$(this.is(":visible"))) {
                   $(this).show();
                   break;
                }
            });

change id to class, like other have said and if you need to cycle the show/hide. 像其他人所说的那样,如果需要循环显示/隐藏,则将id更改为class。 then this might come handy 那么这可能会派上用场

 $('.toAdd').hide();
 var i = 0;
 $('#add input').click(function () {
     i=(i >= 4)?0:i;
      //or if(i >= 4){ i=0;}
     $('.toAdd').eq(i).show().siblings().not(':first').hide();
     i++;

 });

fiddle here 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM