这是我的:
$('#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/