我如何删除我的情况

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我如何删除我的情况相关的知识,希望对你有一定的参考价值。

如何删除javascript中创建的文本li。 li是在javascript中创建的,点击它们前面的每个按钮,它应该只删除li而不是全部

function addBook() {
  var userInput = document.getElementById('books').value;
  if (userInput === "") {
    alert("Please Enter A Text");
    return false;
  }

  var book = document.getElementById('addBook');
  var list = document.getElementById('addBook').children;
  var check = -1;
  var btn = document.createElement('button');
  btn.innerhtml = 'X';

  (list.length === 0) && book.insertAdjacentHTML('beforeend', '<li>' + userInput + '<a class="btn btn-danger btn-sm remove" style="margin-left:10px;" onclick="removeParent(this)">x</a></li>');

  for (var k = 0; k < list.length; k++) {
    console.log(list[k].innerText);
    if (list[k].innerText === userInput + "x") {
      check = 1;
      break;
    }
  }

  (check === -1) && book.insertAdjacentHTML('beforeend', '<li>' + userInput + '<a class="btn btn-danger btn-sm remove" style="margin-left:10px;" onclick="removeParent(this)">x</a></li>');
}

function removeParent(e) {
  var book = document.getElementById('addBook').children;
  book.parentNode.parentNode.removeChild(e.parentNode);

}
<div class="col-md-4 form-cont">
  <h3>Favourite Books </h3>
  <caption>Enter Book's name:</caption> <input type="text" id="books" class="form-control" required="required">
  <button onclick="addBook()" class="btn btn-success">Add Book</button>
  <ol type="i" id="addBook">
  </ol>
  <h3 id="h">Hoobies</h3>
</div>
答案

book不是Element,而是Elements列表

简单地使用e而不是book

function removeParent(e) {
  e.parentNode.parentNode.removeChild(e.parentNode);
}

演示

function addBook() {
  var userInput = document.getElementById('books').value;
  if (userInput === "") {
    alert("Please Enter A Text");
    return false;
  }

  var book = document.getElementById('addBook');
  var list = document.getElementById('addBook').children;
  var check = -1;
  var btn = document.createElement('button');
  btn.innerHTML = 'X';

  (list.length === 0) && book.insertAdjacentHTML('beforeend', '<li>' + userInput + '<a class="btn btn-danger btn-sm remove" style="margin-left:10px;" onclick="removeParent(this)">x</a></li>');

  for (var k = 0; k < list.length; k++) {
    console.log(list[k].innerText);
    if (list[k].innerText === userInput + "x") {
      check = 1;
      break;
    }
  }

  (check === -1) && book.insertAdjacentHTML('beforeend', '<li>' + userInput + '<a class="btn btn-danger btn-sm remove" style="margin-left:10px;" onclick="removeParent(this)">x</a></li>');
}

function removeParent(e) {
  e.parentNode.parentNode.removeChild(e.parentNode);
}
<div class="col-md-4 form-cont">
  <h3>Favourite Books </h3>
  <caption>Enter Book's name:</caption> <input type="text" id="books" class="form-control" required="required">
  <button onclick="addBook()" class="btn btn-success">Add Book</button>
  <ol type="i" id="addBook">
  </ol>
  <h3 id="h">Hoobies</h3>
</div>
另一答案

像这样修改removeParent函数:

function removeParent(e) {
  var book = document.getElementById('addBook');
  book.removeChild(e.parentNode);
}

function addBook() {
  var userInput = document.getElementById('books').value;
  if (userInput === "") {
    alert("Please Enter A Text");
    return false;
  }

  var book = document.getElementById('addBook');
  var list = document.getElementById('addBook').children;
  var check = -1;
  var btn = document.createElement('button');
  btn.innerHTML = 'X';

  (list.length === 0) && book.insertAdjacentHTML('beforeend', '<li>' + userInput + '<a class="btn btn-danger btn-sm remove" style="margin-left:10px;" onclick="removeParent(this)">x</a></li>');

  for (var k = 0; k < list.length; k++) {
    console.log(list[k].innerText);
    if (list[k].innerText === userInput + "x") {
      check = 1;
      break;
    }
  }

  (check === -1) && book.insertAdjacentHTML('beforeend', '<li>' + userInput + '<a class="btn btn-danger btn-sm remove" style="margin-left:10px;" onclick="removeParent(this)">x</a></li>');
}

function removeParent(e) {
  var book = document.getElementById('addBook');
  book.removeChild(e.parentNode);
}
<div class="col-md-4 form-cont">
  <h3>Favourite Books </h3>
  <caption>Enter Book's name:</caption> <input type="text" id="books" class="form-control" required="required">
  <button onclick="addBook()" class="btn btn-success">Add Book</button>
  <ol type="i" id="addBook">
  </ol>
  <h3 id="h">Hoobies</h3>
</div>
另一答案

你只需要使用“e”参数,它直接指向你点击的元素:

<a class="btn btn-danger btn-sm remove" style="margin-left:10px;" onclick="removeParent(this)">x</a>

H

function removeParent(e) {   
    e.parentNode.parentNode.removeChild(e.parentNode);
}

以上是关于我如何删除我的情况的主要内容,如果未能解决你的问题,请参考以下文章

如何删除导航抽屉?

XSS:如何从 C# 中的字符串中删除 JS 片段?

如何从主要活动中替换片段

Android Reorder Fragment Backstack

将片段附加到framelayout时如何删除空格

删除选项卡时在 ActionBar 选项卡片段中运行代码