简体   繁体   English

如何删除元素父级的下一个div?

[英]How can I remove the next div of the parent of an element?

 function yes(event, yesicon) { event.stopPropagation(); yesicon.closest('button').nextSibling.remove(); //Why doesn't this work??? yesicon.closest('button').remove(); //This works ok } 
 <div class="doors_list" id="doors_list"> <button class="accordion"> <div> <img src="Iconos/arrow_down.png" alt="Expand" class="arrow_icon"> </div> <div> <img src="Iconos/yes.png" onclick="yes(event,this);" alt="Yes" class="yes_icon"> </div> <div> <p class="door_room">Kitchen</p> </div> </button> <div class="panel"> --THIS IS THE ONE I WANT TO DELETE -- <p>Info on Door 1</p> </div> </div> 

Inside doors_list I actually have several buttons of class 'accordion' followed by the div of class 'panel' which is the one I want to delete for each one when the 'yes' icon is clicked. 在doors_list内,我实际上有几个“手风琴”类的按钮,然后是“ panel”类的div,单击“是”图标后,我想为每个按钮删除每个div。

What I want to do is to remove the next div of the class 'panel' with the "yes" function. 我要执行的操作是使用“是”功能删除“面板”类的下一个div。

yesicon.closest('button') seems to be working ok but when I want to remove the nextSibling it doesn't seem to work. yesicon.closest('button')似乎工作正常,但是当我想删除nextSibling时,它似乎无法工作。

You have invalid HTML and that is the cause of your problem. 您的HTML无效,这就是问题的原因。

Leaving aside the issue that a <div> is not allowed inside a <button> in the first place… 抛开了首先在<button>内不允许使用<div>的问题……

The content of the button is: 该按钮的内容为:

  • Start div 开始div
  • img img
  • End div 结束div
  • img img
  • End div 结束div

Since there is no open div for the second end tag to close, error recovery implicitly closes the button, and then the div end tag closes the doors_list element. 由于没有用于关闭第二个结束标签的打开div,因此错误恢复将隐式关闭按钮,然后div结束标签关闭doors_list元素。

Consequently: The element you are aiming for is not a sibling of the button, but of its parent. 因此:您要定位的元素不是按钮的同级,而是按钮的父级。

Use a validator . 使用验证器 Write valid HTML. 编写有效的HTML。

You want to use nextElementSibling as it will, as its name implies, be the next sibling element. 您想使用nextElementSibling ,正如其名称所暗示的那样,它将是下一个兄弟元素。 nextSibling will be whatever node follows, and in your case is going to be a text node: the white space characters between your elements. nextSibling将是nextSibling任何节点,并且在您的情况下将是文本节点:元素之间的空白字符。

 function yes(event, yesicon) { event.stopPropagation(); yesicon.closest('button').nextElementSibling.remove(); } 
 <div class="doors_list" id="doors_list"> <button class="accordion"> <div> <img src="Iconos/arrow_down.png" alt="Expand" class="arrow_icon" /> </div> <div> <img src="Iconos/yes.png" onclick="yes(event,this);" alt="Yes" class="yes_icon" /> </div> <div> <p class="door_room">Kitchen</p> </div> </button> <div class="panel"> --THIS IS THE ONE I WANT TO DELETE -- <p>Info on Door 1</p> </div> </div> 

You forgot to open the div before your icon 您忘记在图标前打开div

</div>
 <img src="Iconos/yes.png" onclick="yes(event,this);" alt="Yes" class="yes_icon">
</div>

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

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