javascript - 查询/CSS : How to apply addClass with a delay of 1 sec on mouseenter event.

标签 javascript jquery css

这是我的 addClass 代码,它正在运行。 但是当我尝试使用 delay 或 setTimeout 函数时,它不起作用。

我不想在 CSS 中使用 webkit 属性。

如果有人早些时候遇到过这个问题,请告知。

  $(document).ready(function(){
          $('#table_Id tr td').mouseenter(function(){ 
               $(this).parent('tr').addClass('blueBgColor'); 
            });
  });

最佳答案

使用setTimeout ,它会在指定延迟后调用函数或执行代码片段。

$(document).ready(function() {
    $('#table_Id tr td').mouseenter(function() {
        var self = this; //Cache this in a variable
        setTimeout(function() {
            $(self).parent('tr').addClass('blueBgColor');
        }, 1000); //Here delay in milliseconds
    });
});

编辑:bind 的用法

$(document).ready(function() {
    $('#table_Id tr td').mouseenter(function() {
        setTimeout(function() {
            $(this).parent('tr').addClass('blueBgColor');
        }.bind(this), 1000); //Here delay in milliseconds
    });
});

关于javascript - 查询/CSS : How to apply addClass with a delay of 1 sec on mouseenter event.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25156082/

相关文章:

javascript - 使用 ES6 和 Node.js,推荐的 require 方式

javascript - 在 React Component 的状态中渲染一个对象的属性,在数组中

php - JSON 数据未在 POST 中发送?

html - Div 背景阴影适合自定义 CSS 形状

html - 两个其他 div 之间的自动高度

javascript - 单击选项卡无法准确工作

javascript - 无法在 Polymer 1.0 初学者工具包中创建新路由(通过 page.js 和 iron-page)

javascript - jQuery Accordion 问题

javascript - 防止单元格拖拽

html - 滚动后如何更改背景