如何每4秒在jquery中添加和删除一个类 [英] how to add and remove a class in jquery every 4 seconds
本文介绍了如何每4秒在jquery中添加和删除一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于某种原因,这不是每4秒在post类上添加和删除一个新类。 jquery正确加载,就像这样。 chrome显示代码没有错误。
for some reason, this isn't adding and removing a new class on elements with the class of post, every 4 seconds. jquery loads correctly, as does this. chrome shows no errors with the code.
$(document).ready(function(){
$('.post').addClass('display').delay(4000).removeClass('display');
});
推荐答案
由于您列出了您希望每4秒发生一次你可以简单地使用 setInterval()
Since you listed you want this to happen every 4 seconds you can simply use setInterval()
var $post = $(".post");
setInterval(function(){
$post.toggleClass("display");
}, 4000);
注意,选择器缓存在 $中发布
以最小化每个时间间隔内查询DOM所需的次数。
Note, the selector is cached in $post
to minimize the number of times the DOM needs queried on each interval.
Example on jsfiddle
这篇关于如何每4秒在jquery中添加和删除一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文