我已经制作了一些代码,用if / else测试一些条件,并根据选择的元素添加/删除.hasClass()
,我想知道这段代码是否可以显着减少:
active-class
找到解决方案:
if filter.status == 'all'
@$('.open, .closed, .ended, .resolved').removeClass('active-tab')
@$('.all').addClass('active-tab')
else if filter.status == 'open'
@$('.all, .closed, .ended, .resolved').removeClass('active-tab')
@$('.open').addClass('active-tab')
else if filter.status == 'closed'
@$('.all, .open, .ended, .resolved').removeClass('active-tab')
@$('.closed').addClass('active-tab')
else if filter.status == 'ended'
@$('.all, .open, .closed, .resolved').removeClass('active-tab')
@$('.ended').addClass('active-tab')
else if filter.status == 'resolved'
@$('.all, .open, .closed, .ended').removeClass('active-tab')
@$('.resolved').addClass('active-tab')
答案 0 :(得分:0)
你可以减少它
var selector = '.all, .open, .closed, .ended, .resolved';
@$(selector).not('.'+filter.status).removeClass('active-tab')
@$('.'+filter.status).addClass('active-tab')