[英]Add/remove class while traversing through list-items
我不太明白为什么这行不通。 我试图做到这一点, .on()单击将从列表项中.active-title
class(). .active-title
; 然后将addClass(). .active-title
到next()列表项。
$(".next").on({
click: function(){
$('.active-title').removeClass(function(){
$(this).next().addClass('active-title');
}
});
});
您的代码中有一些错误
}
});
});
应该是
}); <--- closing of removeClass
} <-- closing for click function
});
尝试这个
$(".next").on({
click: function () {
// Find the actuve element
var $active = $('.active-title', 'ul');
// If the next element exists the get the element
// otherwise get the first li from the ul
var $nextElem = $active.next('li').length ? $active.next('li')
: $('ul').find('li:first');
// Add and remove class
$active.removeClass('active-title');
$nextElem.addClass('active-title');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.