jQuery add class for certain time
How I can add a css class to an el开发者_Python百科ement for only 10 seconds ?
A nicely reusable way would be this little jQuery plugin:
(function($){
$.fn.extend({
addTemporaryClass: function(className, duration) {
var elements = this;
setTimeout(function() {
elements.removeClass(className);
}, duration);
return this.each(function() {
$(this).addClass(className);
});
}
});
})(jQuery);
Use like so:
$("#myElement").addTemporaryClass("myClass", 10000);
You can add the class, then call setTimeout(function() { ... }, 10000)
to remove it 10,000 milliseconds later.
Use setTimeout(function,10000);
directly after you set the class on the element to remove the class from that element.
You can add class , and remove it without setTimeout() like this.
$(".count-box", element).addClass("red");
$(".count-box", element).removeClass("red", 10000);
精彩评论