我正在尝试附加一个 onclick 事件,但是它不起作用。不过它在 html 中工作得很好
var a = $("#testappendid").append('<div id="imagewrap"><a href="" id="infobutton" onclick="alert("test");"></a></div>');
var b = $("#testappendid").append('<div id="imagewrap"><a href="" id="infobutton" onclick="RedirectInformation();"></a></div>');
function RedirectInformation() {
alert("test");
}
最佳答案
您可以使用 .on
方法为动态
添加的元素绑定(bind)click
事件处理程序。
$(document).on('click','#infobutton',function(){
//code here.
});
了解更多关于event delegation的信息.
此外,您尝试为两个元素分配相同的 id
,但这是错误的方式:id
属性必须是唯一的。
关于javascript - 附加时 Onclick 事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41902532/