未来元素不会与 on() 绑定

future element doesn't get binded with on()?

本文关键字:绑定 on 元素 未来      更新时间:2023-09-26
    $(".closeBtn").on('click',function(){
        alert('');
    });
+function myfunction(){
    $(".elem").on('click',function(){
        $("#someId").html('<img class="closeBtn" src=""/>'
        );
    });
}();

我使用 html 在某个地方添加了一些元素,我检查了我的 dom 和东西在那里。当我单击绑定的元素 closeBtn 时,它没有响应。我以为使用 on() 会起作用吗?还是关闭问题?

您需要使用:

 $("body").on('click','.closeBtn',function(){
   alert('test');
 });

检查语法 对于使用 on 的事件委派