本文介绍了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 1.7,建议使用新的所有事件绑定目的(无论如何 delegate()
仍然做这个工作)。
Also since jQuery 1.7 it is recommended to use the new on()
for all event binding purposes (anyway delegate()
still does the job).
您可以通过将 hover
与 mouseenter mouseleave
替换 user113716 的解决方案:
You can easily migrate user113716's solution by replacing hover
with mouseenter mouseleave
:
$('mydiv').delegate('seconddiv','mouseenter mouseleave', function(event) {
$(this).toggle( event.type === 'mouseenter' );
});
使用 on()
:
$('mydiv').on('mouseenter mouseleave', 'seconddiv', function(event) {
$(this).toggle( event.type === 'mouseenter' );
});
如果您的问题比简单的切换更复杂
,我建议绑定两个单独的事件:
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
});
这篇关于jQuery是否有.delegate('hover')的句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!