jquery的 on 为什么委托hover 不起作用?
在官网api里查到如下说明:
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.
----
也就是说这个事件从1.9被弃用了。
可以按如下思路实现hover()的代理效果:
$('.container').on('mouseenter mouseout','p',function(){
console.log( event.type );//"mouseenter" or "mouseout" 根据这个标志来切换你的分支
});
JQuery的on()是不支持绑定的hover的,官方API有说
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.
因为在已经被移除了,所以如果想做出移出移入效果,可以参考下面代码:
$('.class').on('mouseenter mouseout',function(event){
console.log( event.type );//"mouseenter" or "mouseout" 根据这个标志来切换你的分支
});
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.
----
也就是说这个事件从1.9被弃用了。
可以按如下思路实现hover()的代理效果:
$('.container').on('mouseenter mouseout','p',function(){
console.log( event.type );//"mouseenter" or "mouseout" 根据这个标志来切换你的分支
});
2014-03-17