我有一个链接列表,一个类活跃.
在我的下一个按钮上单击id就像从当前元素中删除该类并将其添加到下一个我似乎无法添加它?
我做了一个小提琴,希望能解释我的问题,任何帮助都会很棒,谢谢
$('.next').click(function(){
$('ul.pagination').find('a.active').removeClass('active');
$('ul.pagination').find('a.active').next('a').addClass('active');
return false;
});
Run Code Online (Sandbox Code Playgroud)
rai*_*7ow 10
jQuery最常用的一个方面是它的方法(通常)是可链接的 - 换句话说,它们返回它们被调用的对象.所以你可以简单地写一下:
$('ul.pagination').find('a.active').removeClass('active').closest('li')
.next('li').find('a').addClass('active');
Run Code Online (Sandbox Code Playgroud)
......因为它的<li>元素应该是'nexted',而不是<a>那些.但事实上,如果它是最后一个有问题的元素,你不应该完全放弃'主动':
var $a = $('ul.pagination').find('a.active'),
$li = $a.closest('li'),
$nextLi = $li.next('li');
if ($nextLi.length) {
$a.removeClass('active');
$nextLi.find('a').addClass('active');
}
Run Code Online (Sandbox Code Playgroud)
我会这样写,以防止该操作在最后一个 li 上执行任何操作。
$('.next').click(function(e){
e.preventDefault();
if ($("ul.pagination a.active").parent().is(":last-child")) return;
$('ul.pagination a.active').removeClass('active').parent().next().find("a").addClass('active');
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1945 次 |
| 最近记录: |