<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加和删除节点</title>
</head>
<body>
<div id="div1">
<p id="p1">这是一个段落</p>
<p id="p2">这是第二个段落</p>
</div>
<script>
var para = document.createElement(‘p‘);
var node = document.createTextNode(‘这是我又创建的另一个新元素‘);
para.appendChild(node);
var element = document.getElementById(‘div1‘);
var child = document.getElementById(‘p2‘);
// element.appendChild(para);
element.insertBefore(para,child);
element.removeChild(child);
</script>
</body>
</html>
时间: 2024-11-05 21:47:29