【发布时间】:2016-11-16 21:51:44
【问题描述】:
我有一个元素列表,我想在箭头的帮助下切换一个类。两个项目应始终具有“活动”类。单击“下一个”箭头应该会导致下两个项目使类“活动”,当前的两个项目会删除该类。单击“上一个”箭头应该会导致前两个项目使类“活动”,而当前项目会删除该类。
目前我可以让它与“下一个”箭头一起工作,即相应地不断添加类。然而,“前一个”箭头不能正常工作。第一项的类根本不会被删除。 See my fiddle.
代码:
$('.quote:first-of-type').addClass('active');
$('.quote:nth-of-type(2)').addClass('active');
$('.arrow-right').click(function() {
$('.active').removeClass(function(){
if($(this).is('li:last-of-type') ) {
$('.quote:first-of-type').addClass('active');
$('.quote:nth-of-type(2)').addClass('active');
$(this).removeClass('active');
} else {
$(this).nextAll("li:lt(2)").addClass('active');
return 'active';
}
})
});
$('.arrow-left').click(function() {
$('.active').removeClass(function(){
if($(this).is('li:first-of-type')) {
$('.quote:last-of-type').addClass('active');
$('.quote:nth-last-of-type(2)').addClass('active');
$(this).removeClass('active');
} else {
$(this).prevAll("li:lt(2)").addClass('active');
return 'active';
}
})
});
【问题讨论】:
标签: javascript jquery