demo5
test.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Testing</title>
<script type="text/javascript" src="example.js"></script>
</head>
<body>
<div id="testdiv">
</div>
</body>
</html>
example.js
window.onload = function () {
var para = document.createElement("p");
var txt1 = document.createTextNode("This is ");
para.appendChild(txt1);
var emphasis = document.createElement("em");
var txt2 = document.createTextNode("my");
emphasis.appendChild(txt2);
para.appendChild(emphasis);
var txt3 = document.createTextNode(" content.");
para.appendChild(txt3);
var testdiv = document.getElementById("testdiv");
testdiv.appendChild(para);
}
结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/816efe05f780ed95e5996cc169d3482f.png)