2015-02-10 96 views
0

在javascript中加載後很容易在頁面中添加文本,但我想添加一個帶鏈接的鏈接並能夠在不重新加載的情況下捕獲點擊事件頁使用jQuery在頁面內容完全加載後在DOM中添加元素

$("#div").html("<a href='#' id='new'>new link</a>"); 


// not work, because no 
$("#new").click(function(e) { 
    e.preventDefault(); 
    alert('click'); 
}); 

你知道有沒有辦法嗎?

+0

你需要[事件代表團(http://api.jquery.com/on/#direct-and-delegated-events) – 2015-02-10 04:29:27

回答

0

更換

$("#new").click(function(e) { 
    e.preventDefault(); 
    alert('click'); 
}); 

$(document).on('click','#new',function(e) { 
    e.preventDefault(); 
    alert('click'); 
});