悬停事件,元素下降,但不在鼠标上

时间:2012-08-15 04:50:39

标签: jquery

我有以下代码:

$(document).on('hover', '.tic_cont:not(.disabled_ticket)',function () {
        $(this).stop().find(".tic_icon").animate({ top: '-=16px' }, 250);
        $(this).stop().find(".ti_shd").animate({ opacity: '0.25' }, 250);
    }, function () {
        $(this).stop().find(".tic_icon").animate({ top: '+=16px' }, 250);
        $(this).stop().find(".ti_shd").animate({ opacity: '1' }, 250);
    });

当鼠标悬停在div以上时,它必须下降,当鼠标离开时。问题是div总是下降。哪里有问题?
谢谢。
更新

在调试器中,我看到第一个函数没有调用。

1 个答案:

答案 0 :(得分:3)

$(document).on('mouseenter', '.tic_cont:not(.disabled_ticket)',function () {
    $(this).stop().find(".tic_icon").animate({ top: '-=16px' }, 250);
    $(this).stop().find(".ti_shd").animate({ opacity: '0.25' }, 250);
}.on('mouseleave', '.tic_cont:not(.disabled_ticket)', function () {
    $(this).stop().find(".tic_icon").animate({ top: '+=16px' }, 250);
    $(this).stop().find(".ti_shd").animate({ opacity: '1' }, 250);
});

从jQuery 1.8开始不推荐使用:名称“hover”用作字符串“mouseenter mouseleave”的简写。它为这两个事件附加单个事件处理程序,并且处理程序必须检查event.type以确定事件是mouseenter还是mouseleave。不要将“hover”伪事件名称与.hover()方法混淆,后者接受一个或两个函数。 http://api.jquery.com/on