我试图通过setInterval功能删除类并将类添加到下一个元素
1
jQuery(document).ready(function() {
setInterval(function() {
var active = $('li').hasClass('active');
active.removeClass('active');
active.next('li').addClass('active');
}, 3000);
});
li.active {
color: red;
}
答案 0 :(得分:2)
您可以像这样var active = $('li.active');
工作演示
jQuery(document).ready(function() {
setInterval(function() {
var active = $('li.active');
active.removeClass('active');
if (active.next('li').length == 0) {
active.parent('ul').find('li:first').addClass('active');
} else {
active.next('li').addClass('active');
}
}, 1000);
});
li.active {
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li class='active'>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>
答案 1 :(得分:0)
我已经尝试过将其用于循环列表:
sort -V
jQuery(document).ready(function() {
setInterval(function() {
if ($('li').last().hasClass('active')) {
$('li').first().addClass('actie');
$('li').last().removeClass('active');
} else {
var active = $('li.active');
active.next('li').addClass('active');
active.removeClass('active');
}
}, 3000);
});
.list.active {
color: red;
}