<div class="transactionsWrapper">
<input type="button" value="Delete" />
</div>
如果我克隆上面的div让我们说5次,根据删除按钮被点击删除div的jQuery代码是什么?删除克隆的div
<div class="transactionsWrapper">
<input type="button" value="Delete" />
</div>
如果我克隆上面的div让我们说5次,根据删除按钮被点击删除div的jQuery代码是什么?删除克隆的div
$(".transactionsWrapper input:button").click(function() {
$(this).parent().remove();
});
只是...删除输入的父母?
$('input[value=Delete]').on('click', function() {
$(this).parent().remove();
});
编辑
嗯......如果你添加分配事件侦听器后,克隆<div>
元素这是行不通的。最安全的方法是使用委托:
$(document.body).on('click', 'input[value=Delete]', function() {
$(this).parent().remove();
});
这应该工作:
$('input:button[value="Delete"]').click(function() {
$(this).closest('div.transactionsWrapper').remove();
});
这个解决方案有诀窍。 – JONxBLAZE
$(document).on('click', '.transactionsWrapper button', function(){
$(this).parent().remove();
})
如果jQuery版本低于1.7,只需要将'on'函数替换为'delegate'。 – gustavotkg
$(".button").click(function(){
if($(this).parent().hasClass("transactionsWrapper")){
$(this).parent().remove();
}
})
它不会工作,因为他有动态div – Distdev