jquery - 将 on() 与悬停一起使用 - jQuery

标签 jquery hover

这是我的:

$('#blah').hover(function(){
    $('etc').show();
}, function(){
    $('etc').hide();
});

这工作得很好,现在我想要上面的代码与 on() 方法一起工作:

$('#blah').on('hover', function(){
    $('#etc').show();
}, function(){
    $('#etc').hide();
});

但这行不通,有人知道为什么吗?但这也有效:

$('#blah').on('hover', function(){
    $('#etc').show();
});

当我使用 on() 方法时,回调函数不起作用,所以我将 mouseover() 和 mouseleave() 与 on() 一起使用并且它起作用了,我只是想知道为什么悬停回调不起作用使用 on(),这比使用 2 个事件要简单得多....

谢谢

最佳答案

来自 Jquery 文档。 Jquery on

Deprecated as of jQuery 1.8: The name "hover" used as a shorthand for the string "mouseenter mouseleave". It attaches a single event handler for those two events, and the handler must examine event.type to determine whether the event is mouseenter or mouseleave. Do not confuse the "hover" pseudo-event-name with the .hover() method, which accepts one or two functions.

$("div.test").on({
  mouseenter: function(){
    $(this).addClass("inside");
  },
  mouseleave: function(){
    $(this).removeClass("inside");
  }
});

关于jquery - 将 on() 与悬停一起使用 - jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56975064/

相关文章:

当我们将鼠标悬停在下拉菜单上时,CSS li 应该保持事件状态

html - 如何将 <li> 放在 <ul> 之上

javascript - 滚动经过 DOM 元素时触发

javascript - 如何根据下拉列表显示 id?

javascript - ZeroCliboard 不适用于 FF 17 向上

Jquery - 添加 DOM 元素时运行函数

javascript - 可以访问数组元素,但不能迭代

jquery - Jquery 悬停时淡入和淡出图像

css - 在 Chrome 中调整窗口大小时悬停不触发

r - 将多边形添加到散点图,同时保留悬停信息