var t = 0;
var _id;
function categoryCarousel (t){
$(".title li").eq(t).stop().addClass('cur').siblings().removeClass('cur'),
_id = $(".title li").eq(t).attr('id');
var showUl = $("#" + _id + "list ");
showUl.stop().show().siblings("ul").hide();
$("#" + _id + "list .line").show();
}
$(document).on('click', '#next', function () {
if (t > 3 ) {
t = -1;
};
categoryCarousel (++t);
console.log(t)
});
$(document).on('click', '#pre', function () {
if (t < 0) {
t = 3;
};
categoryCarousel (--t)
console.log(t)
});

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
应该是
t=3和t=0的时候不符合if判断,然后执行了++t(t=4)和--t(t=-1)导致的。你看根据你的业务逻辑改为
if(t>=3);if(t<=0);还是额外加一些语句比较好。对于#next的点击事件,如果t>3,t才置为-1,这时候如果t等于3的话,不满足条件就不会执行if里面的语句,然后++t就为4了。而对于#pre的点击事件,如果t<0,t才置为0,这个时候t等于0的话,不满足条件也不会执行if里面的语句, 然后--t就为-1了。