Home  >  Article  >  Web Front-end  >  How to delete the parent node of the current element in javascript

How to delete the parent node of the current element in javascript

青灯夜游
青灯夜游Original
2021-10-13 15:38:405567browse

Javascript method to delete parent node: 1. Find the parent node and grandfather node through the parentNode attribute; 2. Use the removeChild() function to delete the parent node, the syntax is "grandfather node.removeChild (parent node)".

How to delete the parent node of the current element in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

javascript deletes the parent node of the current element

Find the parent node through parentNode and then use removeChild to delete it. Just to delete the parent node, you need to find one more layer, that is, use grandpa to delete the father.

//删除方法
var deleteNode = function (my) {
    //先找到当前节点的父亲
    var parentNode = my.parentNode;
    //找祖父节
    var reprent = parentNode.parentNode;

    reprent.removeChild(parentNode);
}

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to delete the parent node of the current element in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn