如何防止jQuery .remove()删除父类

New*_*ock 5 javascript jquery html5

我已经创建了一个克隆元素函数,可以在这里查看视图演示.按下重置按钮时,它会删除所有克隆的项目元素,但是当尝试在项目列表中添加另一个项目时,使用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();
})
Run Code Online (Sandbox Code Playgroud)

New*_*Boy 2

在有重置按钮的地方,将 if 语句中的代码更改为以下内容

$('.reset').on('click', function(){
    if($('.list-items').length > 1) {
        $('.list-items:last').remove();
    }
})
Run Code Online (Sandbox Code Playgroud)

目前您已按以下方式设置列表项..

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
Run Code Online (Sandbox Code Playgroud)

您需要更改代码,以便它执行以下操作:

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
Run Code Online (Sandbox Code Playgroud)