在页面内容完全加载后使用jQuery在DOM中添加元素

时间:2015-02-10 04:28:21

标签: javascript jquery dom onclick

由于javascript,在加载后很容易在页面中添加文本但是我想添加一个带链接的链接,并能够在不重新加载页面的情况下捕获click事件

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


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

你知道是否有办法吗?

1 个答案:

答案 0 :(得分:0)

替换

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

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