我有这个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>
Run Code Online (Sandbox Code Playgroud)
考虑到最初只有第一个框是可见的.当我点击"向我显示下一个框"时,我希望隐藏当前可见框,并.boxes显示列表中的下一个框.
我认为它唯一接近的就是.each功能,但我认为我不应该div只是为了展示一个而循环.
$('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;
});
Run Code Online (Sandbox Code Playgroud)
现场演示.