简体   繁体   English

尝试删除HTML元素的父级

[英]Trying to delete the parent of a HTML element

I'm trying to delete the parent of a HTML element with the use of JQuery. 我正在尝试使用JQuery删除HTML元素的父级。 The click event concern the span element, i'm trying by then to delete the whole <tr> elements. click事件涉及span元素,我当时正在尝试删除整个<tr>元素。

Here's the DOM tree : 这是DOM树:

{% for element in elements %}
 <tr>
  <th scope=row>Kate</th>
    <td><span id="update">Update</span></td>
    <td id="delete">Delete</td>              
  </tr>
{% endfor %}

And here's the JQuery code trying to delete the parent element of td > span. 这是尝试删除td> span的父元素的JQuery代码。

$('body').on('click','#delete',function(){
   var td = $(this).parent();
   $(td).parent().fadeOut('slow');
});

This code never work for me, so any one to help please. 此代码对我永远都行不通,所以请任何人提供帮助。

first of all don't assign id to the delete button 首先,不要为delete按钮分配id

{% for element in elements %}
 <tr>
  <th scope=row>Kate</th>
    <td><span id="update">Update</span></td>
    <td class="delete">Delete</td>              
  </tr>
{% endfor %}

Now assign the click event handler to the delete class 现在将click事件处理程序分配给delete

$('body').on('click','.delete',function(){
   $(this).parent().parent().fadeOut('slow', function(){
     $( this ).remove();
   });
});

您可以使用:

$(".your_child_element").unwrap()

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

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