【发布时间】:2012-07-15 21:07:56
【问题描述】:
我真的是 Javascript 的初学者,并尝试在此 HTML 中的 div 元素中添加 p 元素:
<!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”
【问题讨论】:
标签: javascript dom append