Javascript删除DOM节点

javascript,dom
Mon Sep 17 2018 08:42:35 GMT+0000 (UTC)

添加节点

var wrapper = document.createElement('div');
wrapper.id = 'wrapper';

var title = document.createElement('h1');
title.id = 'title'
var content = document.createTextNode('hello');
title.appendChild(content);

wrapper.appendChild(title);
document.body.appendChild(wrapper);

删除节点

var parent = document.getElementById('wrapper');
var child = document.getElementById('title')
parent.removeChild(child)

//找到要删除的子元素,然后使用其parentNode属性来找到父元素:
var child=document.getElementById("title");
child.parentNode.removeChild(child);