要删除的节点不是该节点的子节点 JavaScript

The node to be removed is not a child of this node JavaScript

其他堆栈答案未能解决我的问题,因为我认为这是出于不同的原因。我的 JS 代码:

const addButton = document.querySelector('.addButton')
var input = document.querySelector('.input')
const draggable_list = document.getElementById('draggable-list'); //draggable_list is a ul

let itemBox;
let items;
const array = [];
const listItems = [];
let dragStartIndex;

class item {
    constructor(itemName) {
        this.createDiv(itemName);
    }

    createDiv(itemName) {    
        let removeButton = document.createElement('button');
        removeButton.innerHTML = 'REMOVE';
        removeButton.classList.add('removeButton');

        draggable_list.appendChild(items);
        items.appendChild(removeButton);

        removeButton.addEventListener('click', () => this.remove(items);
    }
    async remove(item, value) {
        draggable_list.removeChild(item) 
    }
}

async function check() {
    if (input.value != '') {
        array.push(input.value)
        listItems.push(input.value) 
        array.forEach((numbers,index) => {
            items = document.createElement('li')
            items.setAttribute('data-index', index)
            items.innerHTML = `
            <div class="draggable" draggable="true">
                <p class="phone-number">${numbers}</p>
                <i class="fas fa-grip-lines"></i>
            </div>`;

        } )
        new item(input.value)
        input.value = ''
}

addButton.addEventListener('click', check)

第一次调用remove()时,成功删除了lastli元素。但是当再次调用时,出现以下错误:

Uncaught (in promise) DOMException: Node.removeChild: The node to be removed is not a child of this node

这对你有用吗...

const addButton = document.querySelector('.addButton');
const input = document.querySelector('.input');
const draggable_list = document.getElementById('draggable-list');
//draggable_list is a ul

let itemBox;
let items;
const array = [];
const listItems = [];
let dragStartIndex;

class Item {
  constructor(itemName) {
    this.createDiv(itemName);
  }
  createDiv(itemName) {
    let input = document.createElement('input');
    input.value = itemName;

    let removeButton = document.createElement('button');
    removeButton.innerHTML = 'REMOVE'
    removeButton.classList.add('removeButton');

    draggable_list.appendChild(items);
    items.appendChild(removeButton);

    removeButton.addEventListener('click', (event) => {

      if (event && event.target.parentElement) {
        // this.remove(items));
        this.remove(event.target.parentElement);
      }
    });
  }
  remove(item, value) {
    draggable_list.removeChild(item);
  }
}

function check() {
  if (input.value != '') {
    array.push(input.value);
    listItems.push(input.value);
    array.forEach((numbers, index) => {
      items = document.createElement('li');
      items.setAttribute('data-index', index)
      items.innerHTML = `
            <div class="draggable" draggable="true">
                <p class="phone-number">${numbers}</p>
                <i class="fas fa-grip-lines"></i>
            </div>`;

    });
    new Item(input.value);
    input.value = '';
  }
}

addButton.addEventListener('click', check)
<button class="addButton">+</button>
<input type="text" class="input" />

<ul id="draggable-list">

</ul>

与其复制和粘贴一些您不理解的复杂代码,不如尝试自己编写。尝试专注于您所需要的,别无其他。

这是一种方法:

const [inp,btn,ul]=["input","button","ul"].map(e=>document.querySelector(e));

btn.onclick=function(){
 ul.innerHTML+=`<li><p>${inp.value}</p><button>delete</button></li>`;
 inp.value="";
}

ul.onclick=function(ev){
  if (ev.target.tagName==="BUTTON")
    ul.removeChild(ev.target.closest("li"));
}
<input type="text">
<button>add</button>
<ul></ul>