Javascript jQuery AddClass 然后删除一个类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14447635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery AddClass then removing a class
提问by user1975031
In my Rails application, I send an Ajax request when the user hits the Save button, when it sends the request, I can return some jQuery.
在我的 Rails 应用程序中,当用户点击保存按钮时,我发送一个 Ajax 请求,当它发送请求时,我可以返回一些 jQuery。
What I'd like to do is add a class (saving), have a delay and then remove the class.
我想做的是添加一个类(保存),延迟然后删除该类。
So, I added this:
所以,我添加了这个:
$('.button').addClass('saving').delay(2000).removeClass('saving');
For some reason, it isn't working. What am I doing wrong?
由于某种原因,它不起作用。我究竟做错了什么?
回答by Mark Pieszak - Trilon.io
.delay()is actually for animations.
.delay()实际上是用于动画的。
Use setTimeout()
$('.button').addClass('saving');
setTimeout(function () {
$('.button').removeClass('saving');
}, 2000);
回答by undefined
delayonly works with animation-related methods, you can use queuemethod:
delay仅适用于动画相关的方法,您可以使用queue方法:
$('.button').addClass('saving').delay(2000).queue(function( next ){
$(this).removeClass('saving');
next();
});
回答by Snade
If someone needs more than one adding/removing of the class, .dequeue(); is also requered to function properly.
如果有人需要不止一个添加/删除类,.dequeue(); 还需要正常运行。
$('.button').addClass('saving').delay(2000).queue(function(){
$(this).removeClass('saving');
$(this).dequeue();
});
回答by Dick Grayson
Try this maybe:
试试这个也许:
$('.button').addClass('saving').delay(2000, function() {
$(this).removeClass('saving')
});

