我真的是 Javascript 的初学者,并试图在这个 HTML的元素中添加一个p
元素:div
<!doctype html>
<html>
<head>
</head>
<body>
<p>This is paragraph 1.</p>
<p>This is paragraph 2.</p>
<p>This is paragraph 3.</p>
<p>This is paragraph 4.</p>
<div>
<p id="foo">This is paragraph 5.</p>
</div>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
使用此代码:
(function(){
var divElement = document.getElementsByTagName("div");
el = document.createElement("p");
content = document.createTextNode("This is text");
el.appendChild(content);
document.divElement.appendChild(el);
}());
但我在第 6 行收到此错误:
未捕获的类型错误:无法调用未定义的方法“appendChild”