如何将事件绑定(bind)到脚本加载时不存在的 html 元素?
我的脚本的一部分将这些添加到 DOM:
<a class="btn-remove-item" href="">link</a>
问题是我不能这样做:
$(document).ready(function(){
$(".btn-remove-item").click(function(){
this.parentNode.removeChild(this);
});
});
.. 我认为是因为当页面首次加载时 DOM 元素不存在。
我应该如何将事件绑定(bind)到 myClass?
最佳答案
jQuery.live()已被弃用。这是使用 jQuery.on() 接受的答案相反:
$(document).on('click', '#btn-remove-item', function() {
this.parentNode.removeChild(this);
});
关于jquery - 将事件绑定(bind)到尚未创建的 DOM 元素 (jquery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3392024/