![](/img/trans.png)
[英]How to bind on click event on dynamically created button in Angular 6?
[英]Bind to click-event to dynamically created button without selector
我正在尝试使用javascript和jQuery动态创建元素。 目前,我坚持将简单的点击事件绑定到动态创建的按钮元素。
该按钮使用
var theButton = $("<button>...</button>");
现在,我想绑定点击事件,所以我添加了
theButton.on("click", function(){...}); // which is not working
其他解决方案指出,必须将其绑定到容器。 所以我最终
$(document).on("click", theButton, function(){...}); // this fires no matter where I click
问题是选择器( on
方法的第二个参数)在这一点上是一个jQuery对象。
也尝试过
theButton[0].onclick = () => alert("Clicked")
这也不起作用
尝试这个。 工作正常
$(function(){
var theButton = $("<button>button</button>");
theButton.click(function(){
alert('clicked');
})
$('body').append(theButton);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.