jQuery直播hover

我似乎无法将以下内容转换为实时hover

$("li.favorite_item").hover( function () { $(this).append($(" x")); }, function () { $(this).find("a:last").remove(); } ); 

我试过了:

 $("li.favorite_item"").live('hover', function() { function () { $(this).append($(" x")); }, function () { $(this).find("a:last").remove(); } }); 

但它不起作用。

从jQuery 1.7+ .live() 不推荐使用 .delegate()已被.on()方法取代

使用.on()和.off()代替.live()和.die()。 使用.on()代替.delegate()。

转换旧代码很简单,如此处所述 。


你需要分别调用.hover()映射到的事件,如下所示:

 $("li.favorite_item").live('mouseenter', function() { $(this).append($(" x")); }).live('mouseleave', function () { $(this).find("a:last").remove(); }); 

.hover()不是像.hover()那样的事件函数,它只是.mouseenter(handler1).mouseleave(handler2)的特殊快捷方式 ……所以你需要在.live()做同样的事情.live()打电话。

如果您使用的是jQuery 1.4.3+,则可以使用地图来简化操作,如下所示:

 $("li.favorite_item").live({ mouseenter: function() { $(this).append($(" x")); }, mouseleave: function () { $(this).find("a:last").remove(); } }); 

此外,如果这是在特定的

    .delegate()是一个更好的选择,如下所示:

     $("#myUL").delegate("li.favorite_item", { mouseenter: function() { $(this).append($(" x")); }, mouseleave: function () { $(this).find("a:last").remove(); } }); 

    .live()语法更好,但我们现在必须使用.on()。

    您可以在文档上使用事件映射,将选择器作为第二个参数:

     $(document).on({ mouseenter: function () { $(this).append("x"); }, mouseleave: function () { $(this).find("a:last").remove(); } }, "li.favourite_item"); 

    这是真的…

     $("#your_div_id").live('mouseover',function(){ $(this).find(".child_div").css('background-color','#111111'); }).live('mouseout',function(){ $(this).find(".child_div").css('background-color','#757575'); });