我似乎无法将以下内容转换为实时悬停
$("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();
}
});
但它不起作用。
发布于 2010-12-15 13:01:43
来自jQuery的1.7+ .live()是,而.delegate()已经被.on()方法执行了。
使用.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)
...so,您需要在.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();
}
});
发布于 2012-07-09 15:30:02
.live()语法更好,但现在我们必须使用.on()。
您可以在文档上使用事件映射,并将选择器作为第二个参数:
$(document).on({
mouseenter: function () {
$(this).append("<a href='#' class='button'>x</a>");
},
mouseleave: function () {
$(this).find("a:last").remove();
}
}, "li.favourite_item");
发布于 2013-02-19 23:34:22
这是真的...
$("#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');
});
https://stackoverflow.com/questions/4450125
复制相似问题