jquery定时遍历元素的问题

刚学JQuery,希望实现这样一个效果:在如下的标签中,循环遍历one,two,three,four,每隔1秒钟设置下一个标签的属性,实现类似于“轮询”的效果。
One
Two
Three
Four
我写了一个实现,但是似乎不起作用。请问是什么地方出现了问题?另外还有更佳的实现方式吗?
$(document).ready(function(){
functionselect($pri){
var$current=$pri.next();
$current.radioClass('hilite');
setTimeout(function(){select($current);},1000);
}($('#one'));
});
$.fn.radioClass=function(cls){
returnthis.siblings().removeClass(cls).end().addClass(cls);
}
BIG阳
浏览 330回答 2
2回答

红颜莎娜

1.$(document).ready(function(){functionselect($pri){var$current=$pri.next();$current.radioClass('hilite');setTimeout(function(){select($current);},1000);}($('#one'));//这个写法是有问题的//上述的写法只是声明了一个select函数,进行了一个($('#one'))表达式求值//并没有起到定义后立即执行的效果});//如果可达到你想要的效果$(document).ready(function(){(functionselect($pri){var$current=$pri.next();$current.radioClass('hilite');setTimeout(function(){select($current);},1000);}($('#one')));});2.next方法不会自动回到头部,所以执行完最后一个兄弟节点后,就没有效果了你需要在遍历到最后一个后,执行prev,从后往前遍历,到头后再next

江户川乱折腾

把你想要执行的内容写成fucntion,塞入setTimeout就可以了。$(function(){varselector=$('#onem,#two,#three,#four');vartimeoutCallback=function(){varcurrent=selector.filter('.hilite');varnext=current.next();if(next.length
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript