javascript - Jquery mouseleave setTimeout 不起作用

标签 javascript jquery html

我在 jQuery 中有以下代码:

$( ".name" )
  .on( "mouseenter", function() {
    $(this).find("ul").css({"font-size": "20px",
    'color': "red"});
   })
  .on( "mouseleave", function() {
    setTimeout(function () {
      $(this).find("ul").css({"font-size": "12px",
      'color': "blue"});
    }, 5000);
  });

第一部分工作正常,但第二部分损坏。

为什么是setTimeoutmouseleave不工作?

最佳答案

这是一个范围界定问题。 setTimeout 中的 $(this) 并不引用 $('.name')。您可以通过在正确的级别设置 this 变量并引用该变量来解决此问题。

.on("mouseleave", function() {

    var self = $(this);

    setTimeout(function() {
        self.find("ul").css({
            "font-size": "12px",
            'color': "blue"
        });
    }, 5000);
});

关于javascript - Jquery mouseleave setTimeout 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40323913/

相关文章:

javascript - 在页面加载时打开 jquery 模态对话框

jquery - 在 Bootstrap 下拉列表中添加延迟

javascript - 使用 w3-include-html 将另一个页面上的脚本加载到 div 中时未包含该脚本

javascript - 在选项卡之间切换时如何使用淡入淡出动画?

javascript - 根据用户的选择隐藏/显示新菜单

javascript - 在javascript中创建元素的正确方法

javascript - 使用 this.text() 和 jquery 检索文本

javascript - 如何使用javascript扩展和关闭导航栏移动

javascript - javascript/jquery 中的正则表达式替换

javascript - 工具栏中不显示单选组按钮