<!DOCTYPE html>
<html>
<body>
<script text="type/javascript">
var countdown=function()
{
// create a couple of elements in an otherwise empty HTML page
this .heading=document.createElement("h1");
this .heading_text=document.createTextNode("Big Head!");
this .heading.appendChild(heading_text);
this .document.body.appendChild(heading);
}
var obj1 = new countdown();
</script>
</body>
</html>
在上面的代码中,我无法将appendChild(heading_text)
添加到heading_text属性并面临错误
"Uncaught ReferenceError: heading_text is not defined".
如何继续该计划?
最佳答案
因为 this.heading_text
存在,而 heading_text
不存在
成功
var countdown=function()
{
// create a couple of elements in an otherwise empty HTML page
var heading=document.createElement("h1");
var heading_text=document.createTextNode("Big Head!");
heading.appendChild(heading_text);
document.body.appendChild(heading);
}
关于javascript - 无法将appendchild函数添加到对象中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34528843/