如何使用纯Javascript删除父元素 [英] How to remove the parent element using plain Javascript

查看:89
本文介绍了如何使用纯Javascript删除父元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用纯JavaScript删除父元素和所有各个节点?我没有使用jQuery或任何其他库. 换句话说,我有一个元素,当用户单击它时,我想删除父元素的父元素(以及相应的子节点).

How do I remove the parent element and all the respective nodes using plain JavaScript? I'm not using jQuery or any other library. In other words, I have an element and when user clicks on it, I want to remove the parent of the parent element (as well as the respective children nodes).

<table id='table'>
    <tr id='id'>
        <td>
            Mohit
        </td>   
        <td>
            23
        </td>   
        <td >
        <span onClick="edit(this)">Edit</span>/<span onClick="delete_row(this)">Delete</span>
        </td>   
        <td style="display:none;">
            <span onClick="save(this)">Save</span>
        </td>   
    </tr>   
</table>    

现在

function delete_row(e)
{
    e.parentNode.parentNode.removeChild(e.parentNode);
}

将仅删除最后一个<td>.

如何直接删除<tr>?

e.parentNode.parentNode.getAttribute('id')

返回行的ID ...

returns the id of the row...

是否有remove()delete()之类的功能?

推荐答案

像这样更改您的功能:

function delete_row(e)
{
    e.parentNode.parentNode.parentNode.removeChild(e.parentNode.parentNode);
}

这篇关于如何使用纯Javascript删除父元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆