I want to add a class, wait 2 seconds and add another class.

.addClass("load").wait(2sec).addClass("done");

有办法吗?

推荐答案

setTimeout will execute some code after a delay of some period of time (measured in milliseconds). However, an important note: because of the nature of javascript, the rest of the code continues to run after the timer is setup:

$('#someid').addClass("load");

setTimeout(function(){
  $('#someid').addClass("done");
}, 2000);

// Any code here will execute immediately after the 'load' class is added to the element.

Jquery相关问答推荐

Jquery 表数据无法在 html 上正确显示

使用带有 Ajax GET URL 的数组在每次单击按钮时一次单步执行一个

jQuery .first() 方法返回第一个元素的集合

如何使用 aria-expanded="true" 更改 CSS 属性

如何从 onclick div 属性检测 Javascript 中的 control+click?

在 select2 中使用 AJAX 进行标记

jQuery获取表单字段值

可以在删除类时反转css动画吗?

如何使用 JQuery 发布 JSON 数据?

如何在 HTML选项标签上显示工具提示?

jQuery .load() 调用不会在加载的 HTML 文件中执行 JavaScript

使用jquery设置di​​v标签的值

为什么找不到我的 json 文件?

使用 jQuery 获取选定的选项 id

在 jQuery UI 自动完成上没有检测到结果

滚动 DIV 元素时如何防止页面滚动?

检测 jQuery UI 对话框是否打开

在jQuery中获取列表元素内容的数组

如何确定滚动高度?

如何使用 jquery 更改元素类型