x
 
<!DOCTYPE html>
<html>
<body>
<h1>Element 对象</h1>
<h2>appendChild() 方法</h2>
<p>单击“追加”可创建一个段落并将其附加到 myDIV:</p>
<button onclick="myFunction()">追加</button>
<div id="myDIV">
<h2>我是 myDIV</h2>
</div>
<script>
function myFunction() {
// 创建 p 元素:
const para = document.createElement("p");
// 创建文本节点:
const node = document.createTextNode("This is a paragraph.");
// 把文本节点附加到 p 元素:
para.appendChild(node);
// 把 p 元素附加到 body:
document.getElementById("myDIV").appendChild(para);
}
</script>
</body>
</html>