首先这是我在jQuery代码中的一个例子,我在一个函数中用来做分页:jQuery:在设置HTML内容后立即附加事件
// new_content is a variable that holds the html I want to add to a div
$('div#my_div').html(new_content);
$("div#my_div a.details").hover(function(){
$(this).fadeIn(); //code to execute when the mouse get in
}, function(){
$(this).fadeOut(); //code to execute when the mouse get out
});
但是悬停事件根本不起作用,我相信这是因为DOM还没有准备好然而!
并且为了解决这个问题我设置了这样一个计时器:
$('div#my_div').html(new_content);
window.setTimeout(
$("div#my_div a.details").hover(function(){
$(this).fadeIn(); //code to execute when the mouse get in
}, function(){
$(this).fadeOut(); //code to execute when the mouse get out
});
,100);
我问这个问题因为我确定这不是在html方法之后立即附加事件的正确方法(也许这不是它的工作!)。
si我希望有人告诉我正确的方法。
先谢谢,我只是试过你的例子,这很好,我会在文档中查看细节。 – Hidalgo
你的代码只执行else块中的指令,所以我通过mouseover改变了mouseenter,现在它完美地工作。 – Hidalgo