簡體   English   中英

嘗試刪除HTML元素的父級

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

我正在嘗試使用JQuery刪除HTML元素的父級。 click事件涉及span元素,我當時正在嘗試刪除整個<tr>元素。

這是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 %}

這是嘗試刪除td> span的父元素的JQuery代碼。

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

此代碼對我永遠都行不通,所以請任何人提供幫助。

首先,不要為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 %}

現在將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