简体   繁体   English

Jquery/CSS:如何在 mouseenter 事件上延迟 1 秒应用 addClass。

[英]Jquery/CSS: How to apply addClass with a delay of 1 sec on mouseenter event.

Here is my code to addClass which is working.这是我的 addClass 代码,它正在工作。 But when I try to use delay or setTimeout functions, it is NOT working.但是当我尝试使用延迟或 setTimeout 函数时,它不起作用。

I don't want to use webkit attributes in CSS.我不想在 CSS 中使用 webkit 属性。

Please advise if anybody faced this issue earlier.请告知是否有人早些时候遇到过这个问题。

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

UsesetTimeout , it calls a function or executes a code snippet after a specified delay.使用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
    });
});

EDIT: Usage of bind编辑:使用bind

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

hey i have created a jsfiddle for your problem...嘿,我为您的问题创建了一个 jsfiddle...

code:-代码:-

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

Working example:-工作示例:-

http://jsfiddle.net/L7S37/14/ http://jsfiddle.net/L7S37/14/

thanks谢谢

$(document).ready(function(){
  var myTimeout;
  $('#table_Id tr td').on("mouseenter", function(){
    var that = $(this);
    myTimeout = setTimeout(function () {
      that.parent('tr').addClass('blueBgColor');
    }, 1000);
  });
  // Also if you need, you can do:
  $('#table_Id tr td').on("mouseleave", function(){
    clearTimeout(myTimeout);
  });
});

OR:或者:

$(document).ready(function(){
  var myTimeout;
  // use event.target
  $('#table_Id tr td').on("mouseenter", function (e) {
    myTimeout = setTimeout(function () {
      $(e.target).parent('tr').addClass('blueBgColor');
    }, 1000);
  });
});

You might want to use toggle class if not exists otherwise on every mouseenter the class will be added over and over:如果不存在,您可能希望使用切换类,否则在每次鼠标输入时都会一遍又一遍地添加该类:

$('#table_Id tr td').mouseenter(function(){
    $td = $(this);
    setTimeout(function(){$td.toggleClass('addedClass',true)},'1000');
});

js fiddle example js小提琴示例

Try this尝试这个

 $('#table_Id tr td').mouseenter(function(){
    var $this = $(this); 
    setTimeout(function () {
           $this.parent('tr').addClass('blueBgColor');        
   }, 1000);
});

Try this尝试这个

$('#table_Id tr td').off('mouseenter').on('mouseenter', function() {
    var element = this;
    setTimeout(function() {
        $(element).parent('tr').addClass('blueBgColor');
    }, 1000); /*delay for 1 sec*/
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM