首先這是我在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