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

using jQuery to add element in DOM after page content has fully loaded

本文关键字:jQuery 使用 DOM 元素 添加 加载      更新时间:2023-09-26

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

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

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

你知道有没有办法吗?

替换

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

带有

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