我的代码有问题,我有一些按钮在被调用时执行一个函数。
<button class="executeFucntion" > Example </button>
那个可以工作,但我还有其他动态创建按钮的按钮:
$("<button class='executeFucntion'>Example</button>").insertBefore("#id")
这个按钮,第二个按钮,不能与下一个功能一起使用,我不知道为什么:
$(".executeFucntion").on("click", function(e) {
//Code here
});
有什么帮助吗? 谢谢。
最佳答案
对于动态添加的元素,请使用 event delegation
.
$(document).on("click", ".executeFucntion", function (e) {
//Code here
});
Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future.
关于javascript - .在 ("click"上,功能无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30807381/