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

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

问题描述

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

 < table id ='table'> 
< tr id ='id'>
< td>
Mohit
< / td>
< td>
23
< / td>
< td>
< / td>
< td style =display:none;>
< span onClick =save(this)>保存< / span>
< / td>
< / tr>
< / table>

现在,

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

只会移除最后一个< td>



如何直接删除< tr> >?

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

返回行的id ...

是否有像 remove() delete()

解决方案

更改你的功能如下:

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


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>    

Now,

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

Will remove only last <td>.

How do I remove the <tr> directly>?

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

returns the id of the row...

Is there any function like remove() or delete() ?

解决方案

Change your function like this:

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

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

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