我尝试使用javascript和jQuery动态创建元素。目前,我坚持将简单的点击事件绑定到动态创建的按钮元素。
按钮是用
创建的Option::unwrap
现在我想绑定click事件,所以我添加了
var theButton = $("<button>...</button>");
其他解决方案指出,它必须绑定到容器。所以我最终得到了
theButton.on("click", function(){...}); // which is not working
问题是选择器($(document).on("click", theButton, function(){...}); // this fires no matter where I click
方法的第二个参数),此时它是一个jQuery对象。
也试过
on
也无效
答案 0 :(得分:1)
试试这个。它正在运作
$(function(){
var theButton = $("<button>button</button>");
theButton.click(function(){
alert('clicked');
})
$('body').append(theButton);
})