0
我想动态创建使用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
方法的第二PARAM),其是在这点上一个jQuery对象。
也试过
theButton[0].onclick =() => alert("Clicked")
这也不能正常工作