[英]what's the best way to add a class to the div with a class of post every 4 seconds using jquery?
what's the best way to add a class to the div with a class of post every 4 seconds using jquery?使用 jquery 每 4 秒将 class 添加到 div 中的最佳方法是什么?
<div class="34 post">
<img width="311" height="417" src="#" class="#" alt="newspapers" />
<h2><a href="#">Headline News Part 2</a></h2>
<p>testing new content</p>
</div>
<div class="9 post">
<img width="311" height="417" src="#" class="#" alt="newspapers" />
<h2><a href="#">Headline News Part 2</a></h2>
<p>testing new content</p>
</div>
<div class="6 post">
<img width="311" height="417" src="#" class="#" alt="newspapers" />
<h2><a href="#">Headline News Part 2</a></h2>
<p>testing new content</p>
</div>
so i want the first to have a class of "display" then after 4 seconds, i want to remove the class on that one and add it to the second one.所以我希望第一个有一个 class 的“显示”,然后在 4 秒后,我想删除那个上的 class 并将其添加到第二个。 and then after 4 more seconds, remove it from the second and add it to the third.然后再过 4 秒后,将其从第二个中删除并将其添加到第三个中。 when it gets to the end it loops back around.当它到达尽头时,它会循环回来。
var $postDivs = $('div.post'), // assuming these won't change
i = -1,
CLASS_NAME = 'foo';
setInterval(function () {
$postDivs.eq(i).removeClass(CLASS_NAME);
i = (i+1) % $postDivs.length;
$postDivs.eq(i).addClass(CLASS_NAME);
}, 4000);
http://jsfiddle.net/mattball/k2sJy/ http://jsfiddle.net/mattball/k2sJy/
If you want to create a simple infinite rotation of news items, you may try jQuery cycle plugin for doing this.如果你想创建一个简单的无限循环的新闻项目,你可以试试jQuery 循环插件来做这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.