javascript - 无法将appendchild函数添加到对象中的属性

标签 javascript dom

<!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/

相关文章:

javascript - 如何将整个 .js 文件的变量内容保存并加载到用户计算机上?

javascript - 有没有办法检测外部 js 何时加载和执行?

javascript - 如何在JS中的文本节点(document.createTextNode ('...'))中添加span元素?

javascript - 禁用 parentNode -jquery 的所有输入标签

python - 你如何使用 python 从 <abc>some Text</abc> 获取 "some Text"?

javascript - 为什么jQuery或诸如getElementById之类的DOM方法找不到元素?

javascript - vue.js 入门不起作用

javascript - 如何在 Kendo Grid 的模态窗口中显示文件内容

javascript - 单击按钮后 Chart.js 为图表添加动画

javascript - 如何从网页中获取文本数据