我创建了一个可以查看的克隆元素函数view demo here .当按下重置按钮时,它会删除所有克隆的项目元素,但是当尝试向项目列表添加另一个项目时,"new"添加的项目在 DOM 中不可见。
$('#add-btn').on('click',function(){
$('.list-items:first').clone().appendTo("#items").addClass('isVisible');
$('#items-fields').val('');
})
// RESET BUTTON
$('.reset').on('click', function(){
if( $('.list-items').length != 1);
$('.list-items:last').remove();
event.preventDefault();
})
最佳答案
在重置按钮所在的位置,将 if 语句中的代码更改为以下内容
$('.reset').on('click', function(){
if($('.list-items').length > 1) {
$('.list-items:last').remove();
}
})
目前您已按以下方式设置列表项..
When a user clicks the delete button, if the number of things with the class list-item does not equal 0, then remove the last list-item
您需要更改它的代码,以便它执行以下操作:
When a user clicks the delete button, if the number of things with the class list-item is greater than 1, then remove the last list-item
关于javascript - 如何防止 jQuery .remove() 删除父类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34406447/