2个回答

9

使用mouseentermouseleave代替mouseovermouseout

示例代码

$(document).ready(function(e) {
    $('ul.dropit li').on('mouseenter', function(event) {
        $target = $(event.currentTarget);
        $sub = $target.children('ul').first();
        $sub.slideToggle();
    }).on('mouseleave', function(event) {
        $target = $(event.currentTarget);
        $target.children('ul').first().slideToggle();
    });
});​

mouseover 当鼠标悬停在子元素上时触发。

mouseenter 无论有多少个子元素被悬停,都只会触发一次。


5

jsFiddle演示

只需使用.hover()

而且,为了防止重复的悬停动作导致丑陋的幻灯片堆积,请使用:.stop()

$('.dropit li:has("ul")').hover(function() {
    $('>ul', this).stop().slideToggle();
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接