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

标签 javascript jquery html

我创建了一个可以查看的克隆元素函数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/

相关文章:

javascript - addEventListener 在 CreateJS 中不起作用

javascript - 相当于 jQuery 加载的原型(prototype)

Jquery: append 表单输入值 (.val)

javascript - Jquery 选择 css 类 :before

iphone - 不同设备和模拟器的颜色看起来不同

javascript - 表格内容不在下方

javascript - Jquery .val() 通过按下按钮改变变量值

javascript - 用于检查输入是否有任何值的 jQuery 脚本。 IE 和 Chrome 中的问题

javascript - 在 Canvas 中更改颜色图像

javascript - 选择选项的工具提示或评论