内容简介:翻译自:https://stackoverflow.com/questions/4450125/jquery-live-hover
我似乎无法将以下内容转换为实时悬停
$("li.favorite_item").hover( function () { $(this).append($(" <a href='#' class='button'>x</a>")); }, function () { $(this).find("a:last").remove(); } );
我试过了:
$("li.favorite_item"").live('hover', function() { function () { $(this).append($(" <a href='#' class='button'>x</a>")); }, function () { $(this).find("a:last").remove(); } });
但它不起作用.
.
使用 .on() 和 .off() 代替.live()和.die().使用.on()代替.delegate().
转换旧代码很简单 as explained here .
您需要分别调用
.hover()
映射到的事件,如下所示:
$("li.favorite_item").live('mouseenter', function() { $(this).append($(" <a href='#' class='button'>x</a>")); }).live('mouseleave', function () { $(this).find("a:last").remove(); });
.hover()
不是像
.click()
这样的事件函数,例如,它是
just a special shortcut for .mouseenter(handler1).mouseleave(handler2)
…所以你需要在你的
.live()
调用中做同样的事情.
如果您使用的是jQuery 1.4.3,则可以使用地图来简化操作,如下所示:
$("li.favorite_item").live({ mouseenter: function() { $(this).append($(" <a href='#' class='button'>x</a>")); }, mouseleave: function () { $(this).find("a:last").remove(); } });
此外,如果这是特定的<ul>,
.delegate()
是更好的选择,如下所示:
$("#myUL").delegate("li.favorite_item", { mouseenter: function() { $(this).append($(" <a href='#' class='button'>x</a>")); }, mouseleave: function () { $(this).find("a:last").remove(); } });
翻译自:https://stackoverflow.com/questions/4450125/jquery-live-hover
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Think Python
Allen B. Downey / O'Reilly Media / 2012-8-23 / GBP 29.99
Think Python is an introduction to Python programming for students with no programming experience. It starts with the most basic concepts of programming, and is carefully designed to define all terms ......一起来看看 《Think Python》 这本书的介绍吧!