JQuery用load加载进来的内容上为什么没事件呢?

在主页面上我个UL下面的LI加了hover:
[code="javascript"]
$("#postList li").hover(
function () {
$(this).find("div.toolbar").show()
$(this).css("background", "#FFDD43");
},
function () {
$(this).find("div.toolbar").hide();
$(this).css("background", "transparent");
}
);
[/code]
后面做了一些操作以后,要把UL里面的内容重新加载,我用load:
[code="javascript"]
$("UL").load("/reload", function(){
$("#postCount").text($("UL li").length);
$("form[@name='newpost']")[0].reset();
});
[/code]
内容现在都正常加载进来的,但是hover却没了。这是为什么呢?
我在后面把hover重新注册一下,就好了,有没有更好的方法呢?
[code="javascript"]
$("#postList").load("/reload", function(){
$("#postCount").text($("#postList li").length);
$("form[@name='newpost']")[0].reset();
$("#postList li").hover(
function () {
$(this).find("div.toolbar").show()
$(this).css("background", "#FFDD43");
},
function () {
$(this).find("div.toolbar").hide();
$(this).css("background", "transparent");
}
);
});
[/code]

你load 后 DOM Tree上受影响的对象重构了,对应的Element已经被新的替换了,老的事件所对应的Element已经脱离DOM Tree,是应该重新注册一下.
不过你这样的方法是否会造成内存泄漏还有待考证.

这个是使用事件处理程序与ajax请求时的一个常见问题,加载的元素必须在适当的时机单独绑定自己的事件处理程序。所以需要重新注册一下。