【发布时间】:2019-09-16 15:36:35
【问题描述】:
我想根据名称列表(var "items")每 2 秒更改一次类的名称。
我找到了一个很好的 SO here 并尝试根据我的需要进行更改。它现在对我有用,但我不知道如何删除 $text 和 div "#div1" - 因为我不需要它。在另一个 SO 中,它用于更改 div 的 html 文本。但我不需要这个,我只想更改该部分的类名 - 我试图删除此代码,但它不再起作用了。
HTML:
<section class="steps">class name of this section changes (see inspector)</section>
<div id="div1"></div> <!-- this could be removed -->
<button>Stop it Now.</button>
JS:
$(document).ready(function() {
var items = ["one", "two", "three"],
$text = $( '#div1' ),
$step = $( 'section.steps' ),
delay = 2; //seconds
function loop ( delay ) {
$.each( items, function ( i, elm ){
$text.delay( delay*1E3).hide();
$text.queue(function(){
$text.html( items[i] );
$text.dequeue(); // this is not needed
$step.addClass( items[i] ).delay(2000).queue(function(){
$(this).removeClass( items[i] ).dequeue();
});
});
$text.show();
$text.queue(function(){
if ( i == items.length -1 ) {
loop(delay);
}
$text.dequeue();
});
});
}
loop( delay );
$("button").on("click", function() {
$text.stop(true, false);
})
});
【问题讨论】:
-
在停止函数调用之后,把这个:
$text.remove(); -
我最后的小提琴,感谢 bgaynor78:jsfiddle.net/ary8k30z
标签: javascript jquery loops