jQuery 是否有 .delegate('hover') 的句柄? [英] Does jQuery have a handleout for .delegate('hover')?

查看:26
本文介绍了jQuery 是否有 .delegate('hover') 的句柄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用:

$('mydiv').delegate('hover', function() {  
    $('seconddiv').show();  
}, function() {  
    //For some reason jQuery won't run this line of code  
    $('seconddiv').hide();  
});

推荐答案

User113716 的出色答案将不再适用于 jQuery 1.9+,因为 伪事件 hover 不再受支持(升级指南).

User113716's great answer will no longer work in jQuery 1.9+, because the pseudo-event hover is no longer supported (upgrade guide).

另外,由于 jQuery 3.0 delegate() 已正式弃用绑定事件,所以请使用 new on()(docs) 为所有事件绑定目的.

Also since jQuery 3.0 delegate() for binding events is officially deprecated, so please use the new on()(docs) for all event binding purposes.

您可以轻松迁移 user113716 的解决方案,只需将 hover 替换为 mouseenter mouseleave 并切换到 on()语法:

You can easily migrate user113716's solution by replacing hover with mouseenter mouseleave and switching to the on() syntax:

$('mydiv').on('mouseenter mouseleave', 'seconddiv', function(event) {
    $(this).toggle( event.type === 'mouseenter' );  
});

如果您的问题比简单的 toggle 更复杂,我建议绑定两个单独的事件:

If your problem is more complex than a simple toggle, I suggest binding two separate events:

$('mydiv').on('mouseenter', 'seconddiv', function( event ) {
    // do something
}).on('mouseleave', 'seconddiv', function( event ) {
    // do something different
});

注意:由于 hover 在 v1.9 中被移除,而 on() 在 v1.7 中被引入,所以没有真正的需要对于使用 delegate() 的解决方案 - 但如果您出于某种原因喜欢它;它仍然存在(目前)并且可以完成工作:

NB: Since hover was removed in v1.9 and on() was introduced in v1.7, there is no real need for a solution using delegate() - but if you like it for some reason; it is still there (for now) and does the job:

$('mydiv').delegate('seconddiv','mouseenter mouseleave', function(event) {
    $(this).toggle( event.type === 'mouseenter' );  
});

这篇关于jQuery 是否有 .delegate('hover') 的句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆