简体   繁体   English

使用子元素删除父元素

[英]Remove Parent Element with Child Element

I am trying to remove a div when a user clicks on the remove button, but as of now it only removes the button, how can I make it remove the entire div that it is held in? 我正在尝试在用户单击“删除”按钮时删除div,但是截至目前,它仅删除了该按钮,如何使它删除所保存的整个div? I have added my attempt below, as you can see I remove the button and attempt to remove the entire div that it is held in but I have not been able to. 我在下面添加了我的尝试,如您所见,我删除了该按钮,并尝试删除了该按钮所在的整个div,但我未能做到。 I have tried to remove the parentElement but that did not work either, I am not sure what I have done wrong. 我曾尝试删除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> 

You need to use this line of code: 您需要使用以下代码行:

this.parentElement.remove();

instead of 代替

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

What are you doing wrong? 你在做什么错

You're selecting the current element's parent's child which is itself (the button you're clicking). 您正在选择current element's parent's child which is itself (您正在单击的按钮)。 That's why it is removing the button instead of it's parent that is the div container. 这就是为什么要删除按钮而不是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> 

Create a new attribute to the button and set its value as the parent div. 为按钮创建一个新属性,并将其值设置为父div。 On click of the button get this value. 单击按钮后,获取此值。 Since this value is same as the id of the parent , use it to remove from the dom.Also not the id of each element need to be unique 由于此值与父代的ID相同,因此可使用它从dom中删除。每个元素的ID不必唯一

 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 = 'noteBoxes' + 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; // adding a new attribute to data-parent-id remove.setAttribute('data-parent-id', 'noteBoxes' + noteCount); remove.onclick = function() { // get the id of the parent on click var getParentId = this.getAttribute('data-parent-id') document.getElementById(getParentId).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"></button> </div> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM