javascript - 移除带有子元素的父元素

标签 javascript jquery html css

我试图在用户点击删除按钮时删除一个 div,但到目前为止它只删除了按钮,我怎样才能让它删除它所在的整个 div?我在下面添加了我的尝试,如您所见,我删除了按钮并尝试删除它所在的整个 div,但我无法做到。我已经尝试删除 parentElement 但这也不起作用,我不确定我做错了什么。

var noteCount = 0;

function addNote(style) {

  const notesBox = document.getElementById('notesBox');
  var noteBoxes = document.createElement('div');
  textarea = document.createElement('textarea'),
    remove = document.createElement('button'),
    today = new Date(),
    txt = document.createTextNode('' + today.toLocaleDateString() + ' ' + today.getHours() + ':' + today.getMinutes());

  notesBox.appendChild(noteBoxes);

  noteBoxes.setAttribute('class', style);
  noteBoxes.className = 'noteBoxes';
  noteBoxes.setAttribute('id', style);
  noteBoxes.id = 'note box ' + noteCount;
  noteBoxes.appendChild(textarea);
  noteBoxes.appendChild(remove);

  textarea.appendChild(txt);
  textarea.setAttribute('class', style);
  textarea.className = 'notesE';
  textarea.setAttribute('id', style);
  textarea.id = 'note' + noteCount;

  remove.setAttribute('class', style);
  remove.className = 'removeNote';
  remove.setAttribute('id', style);
  remove.id = '-Note' + noteCount;
  remove.onclick = function() {
    this.parentElement.removeChild(this);
    this.noteBoxes.removeChild(this.noteBoxes);
  }
  noteCount++;
  console.log(textarea.id);

}
<div id="custNotes" style=" width: 520px; margin: 0 auto;">
  <h3>Notes</h3>
  <button class="options" onclick="addNote()" style="margin-bottom: 10px;">add</button>
  <div class="notesScroll" style="width: 550px; background-color: #606060; margin: 0 auto;">
    <div id="notesBox" style="padding: 10px; width: 510px;">
      <div id="noteBoxes">
        <textarea class="notesE"></textarea>
        <button class="removeNote"></button>
      </div>
    </div>
  </div>
</div>

最佳答案

你需要使用这行代码:

this.parentElement.remove();

代替

this.parentElement.removeChild(this);
this.noteBoxes.removeChild(this.noteBoxes);

你做错了什么?

您正在选择 当前元素的父元素的子元素本身(您正在单击的按钮)。这就是为什么它要删除按钮而不是它的父元素,即 div 容器。

var noteCount = 0;

function addNote(style) {

  const notesBox = document.getElementById('notesBox');
  var noteBoxes = document.createElement('div');
  textarea = document.createElement('textarea'),
    remove = document.createElement('button'),
    today = new Date(),
    txt = document.createTextNode('' + today.toLocaleDateString() + ' ' + today.getHours() + ':' + today.getMinutes());

  notesBox.appendChild(noteBoxes);

  noteBoxes.setAttribute('class', style);
  noteBoxes.className = 'noteBoxes';
  noteBoxes.setAttribute('id', style);
  noteBoxes.id = 'note box ' + noteCount;
  noteBoxes.appendChild(textarea);
  noteBoxes.appendChild(remove);

  textarea.appendChild(txt);
  textarea.setAttribute('class', style);
  textarea.className = 'notesE';
  textarea.setAttribute('id', style);
  textarea.id = 'note' + noteCount;

  remove.setAttribute('class', style);
  remove.className = 'removeNote';
  remove.setAttribute('id', style);
  remove.id = '-Note' + noteCount;
  remove.onclick = function() {
    this.parentElement.remove();
  }
  noteCount++;
  console.log(textarea.id);

}
<div id="custNotes" style=" width: 520px; margin: 0 auto;">
  <h3>Notes</h3>
  <button class="options" onclick="addNote()" style="margin-bottom: 10px;">add</button>
  <div class="notesScroll" style="width: 550px; background-color: #606060; margin: 0 auto;">
    <div id="notesBox" style="padding: 10px; width: 510px;">
      <div id="noteBoxes">
        <textarea class="notesE"></textarea>
        <button class="removeNote">Remove</button>
      </div>
    </div>
  </div>
</div>

关于javascript - 移除带有子元素的父元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47911414/

相关文章:

javascript - 允许背景图像 url 可点击

javascript - 在 Firefox 中打印代码但在 IE 中不打印

jquery - 如何使用 jQuery 从 DIV 中删除高度样式?

javascript - 电子邮件地址和电话验证 | jQuery

c# - 如何在 HTML 表格上复制 "freeze pane"功能?

javascript - 带 JQuery 的工具提示事件和带 id 的数组

php - 自定义验证消息不起作用?

c# - UnicodeCategory.Other正则表达式的字母范围

javascript - 尝试压缩 JavaScript/jQuery 代码

javascript - 如何从嵌套函数中中断 JavaScript for 循环