2015-02-10 116 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'); 
}); 
相关问题