2 回答
TA贡献1757条经验 获得超8个赞
下面的代码使 onclick 具有循环的能力,即使从开始到结束也是如此:
$(function(){
// store all items
var allItems = $(".nav-right").children();
// initialize for loop counter
var i = 0;
// onclick
$("#next-item").on("click", function(){
// select the current item
var wrapper = document.querySelector('.current');
// removing the current wrapper
wrapper.outerHTML = wrapper.innerHTML;
// looping and wrap the next item with current div
i = (i + 1) % allItems.length;
allItems.eq(i).wrapInner('<div class="current"></div>');
});
});
根据@daniel-beck 的建议更新:
$(function(){
// store all items
var allItems = $(".nav-right").children();
// initialize for loop counter
var i = 0;
// onclick
$("#next-item").on("click", function(){
// select the current item and remove the current wrapper
$('.current').contents().unwrap();
// looping and wrap the next item with current div
i = (i + 1) % allItems.length;
allItems.eq(i).wrapInner('<div class="current"></div>');
});
});
添加回答
举报