开发者

Cycle through divs?

I have this HTML:

<div id="container">

<div class="boxes">first box</div>
<div class="boxes">second box</div>
<div class="boxes">third box</div>

</div>

<a href="jquery">Show me next box</a>

Consider that initially only the first box is visible. When I click 'Show me next box' I want the current visible box to be hidden, and the next .boxes on the list to appear.

The only thing that I think it gets cl开发者_JAVA百科ose is the .each function, but I shouldn't cycle through all divs just to show one, I think.


$('a').click(function() {
    var visibleBox = $('#container .boxes:visible');
    visibleBox.hide();
    var nextToShow = $(visibleBox).next('.boxes:hidden');
    if (nextToShow.length > 0) {
        nextToShow.show();
    } else {
        $('#container .boxes:hidden:first').show();
    }
    return false;
});

Live demo.


$('a').click(function(){
    $('.boxes').filter(':visible').hide().next().add('.boxes:first').last().show();
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜