代码示例:(标识:jsck_node_removechild_5)
<!DOCTYPE html>
<html>
<body>
<h1 id="myH1">Element 对象</h1>
<h2>removeChild() 方法</h2>
<iframe src="/static/demo/html/removechild.html" style="height:400px;width:650px;"></iframe>
<p>单击按钮将 h1 从其父文档中删除并将其插入到其他文档中。</p>
<button onclick="remove()">删除</button>
<button onclick="insert()">插入</button>
<script>
const child = document.getElementById("myH1");
function remove() {
child.parentNode.removeChild(child);
}
function insert() {
const frame = document.getElementsByTagName("IFRAME")[0]
const h = frame.contentWindow.document.getElementsByTagName("H1")[0];
const x = document.adoptNode(child);
h.appendChild(x);
}
</script>
</body>
</html>