jQuery 如何将 onclick 事件绑定到动态添加的 HTML 元素 [英] jQuery how to bind onclick event to dynamically added HTML element
问题描述
我想将 onclick 事件绑定到我使用 jQuery 动态插入的元素
但它从不运行绑定函数.如果你能指出为什么这个例子不起作用以及我如何让它正常运行,我会很高兴:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="da" lang="da"><头><title>点击绑定测试</title><script src="https://code.jquery.com/jquery-1.12.4.min.js"></script><script type="text/javascript">jQuery(函数(){close_link = $('<a class="" href="#">单击此处查看警报</a>');close_link.bind("点击", function(){alert('来自绑定函数调用的你好');//在这里做些事情...});$('.add_to_this').append(close_link);});头部><身体><h1 >点击绑定测试</h1><p>问题:将点击事件绑定到我通过 JQuery 附加的元素.</p><div class="add_to_this"><p>链接被创建,然后在下面添加:</p>
<div class="add_to_this"><p>下面添加了另一个:</p>