为什么“.on(< event>,function())”悬停的jQuery事件处理程序的样式不起作用? [英] Why does the ".on(<event>, function())" style of jQuery event handler not work for "hover"?
问题描述
这两个 .hover(function(){...},function(){...})
使用元素ID或这个
工作:
Both of these .hover(function() {...}, function() {...})
using either the element ID or this
work:
$( "#imgPostTravel" ).hover(function() {
$('#imgPostTravel').addClass('popout_image');
$('#imgPostTravel').addClass('shadow');
}, function() {
$('#imgPostTravel').removeClass('popout_image');
$('#imgPostTravel').removeClass('shadow');
});
$( "#imgPostTravel" ).hover(function() {
$(this).addClass('popout_image');
$(this).addClass('shadow');
}, function() {
$(this).removeClass('popout_image');
$(this).removeClass('shadow');
});
...而使用 .on(hover,function() {...}
:
$( "#imgPostTravel" ).on( "hover", function() {
$('#imgPostTravel').addClass('popout_image');
$('#imgPostTravel').addClass('shadow');
}, function() {
$('#imgPostTravel').removeClass('popout_image');
$('#imgPostTravel').removeClass('shadow');
});
...不。
为什么不?
推荐答案
这是因为 hover
不是事件名称jQuery 1.9的库支持使用这个名称,如附加说明中所述,这里:
That's because hover
is not an event name. Prior to jQuery 1.9 the library supported using this name, as is stated in the "Additional notes here:
在jQuery 1.8中弃用,1.9中删除:名称hover用作字符串mouseenter mouseleave的缩写它连一个偶也t处理程序,而处理程序必须检查event.type以确定事件是mouseenter还是mouseleave。不要将hover伪事件名称与接受一个或两个函数的.hover()方法混淆。
Deprecated in jQuery 1.8, removed in 1.9: 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.
既然它不是一个标准的事件名称,而且一旦存在的特殊情况支持就已经没有了,它根本不起作用。
Since it's not a standard event name, and the "special case support" that once existed is gone, it simply doesn't work...
这篇关于为什么“.on(< event>,function())”悬停的jQuery事件处理程序的样式不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!