简体   繁体   English

jQuery函数.on适用于1.8.3但不适用于1.9.1

[英]jQuery function .on works in 1.8.3 but not in 1.9.1

I have a function that works with jQuery 1.8.3 but when I upgrade to 1.9.1 it's not working anymore, but there's no change in documentation. 我有一个与jQuery 1.8.3一起使用的功能但是当我升级到1.9.1时它不再工作了,但是文档没有变化。 Does somebody know how to fix this? 有人知道如何解决这个问题吗?

$(document).on("hover", "#cart-left", function(){
    $("#cart").addClass('active');
});

http://jsfiddle.net/michalcerny/R9HZp/ http://jsfiddle.net/michalcerny/R9HZp/

Thanks for support! 感谢你的支持!

In 1.9.1 you should use mouseover 在1.9.1中,您应该使用mouseover

$(document).on("mouseover", "#cart-left", function(){
    $("#cart").addClass('active');
});

The status of the hover shorthand hover速记的状态

As of jQuery 1.8 the hover shorthand has been deprecated . 从jQuery 1.8开始, hover速记已被弃用 See the jQuery on() documentation : 请参阅jQuery on()文档

Deprecated as of jQuery 1.8: The name "hover" used as a shorthand for the string "mouseenter mouseleave" 自jQuery 1.8开始不推荐使用:名称“hover”用作字符串“mouseenter mouseleave”的简写

As of jQuery 1.9, the hover shorthand is unsupported . 从jQuery 1.9开始, hover速记不受支持 See the jQuery 1.9 Upgrade Guide 请参阅jQuery 1.9升级指南

Alternative 替代

In your case, it means you should use the mouseenter event. 在您的情况下,这意味着您应该使用mouseenter事件。 For example: 例如:

$(document).on("mouseenter", "#cart-left", function(){
     $("#cart").addClass('active');
});

See jsFiddle demo 请参阅jsFiddle演示

Making better usage of on() 更好地利用on()

It's also worth noting that unless the selector passed to on() refers to elements that are added to the DOM dynamically (ie after page load), there's no need to delegate the handler to the document . 值得注意的是,除非传递给on()的选择器引用动态添加到DOM的元素(即在页面加载之后),否则不需要将处理程序委托document Instead, in this case you can probably bind the handler function directly to the element like so: 相反,在这种情况下,您可以处理函数直接绑定到元素,如下所示:

$("#cart-left").on("mouseenter", function(){
    $("#cart").addClass('active');
});

问题不在于“on”函数,而是在1.9.1中已弃用(和删除)的“hover”伪事件: http//jquery.com/upgrade-guide/1.9/#hover-伪事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM