我对JS和Web开发世界都很陌生,所以如果我的问题有点乏味,请提前道歉.
我写了这段代码:
var breakfast = new Object();
breakfast.food = "croissant";
breakfast.extra = ["mushroom", "cheese"];
breakfast.eat = function(){return this.food + " with " + this.extra[0];}
var elBreakfast = document.getElementById("breakf");
elBreakfast.textContent = breakfast.eat();
Run Code Online (Sandbox Code Playgroud)
我从浏览器收到错误消息:
"Uncaught TypeError: Cannot set property 'textContent' of null"...
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
谢谢!
这里唯一重要的代码是document.getElementById("breakf");.您的浏览器似乎无法查询ID为breakfHTML标记的元素.
因此,您需要在浏览器中检查HTML代码.检查是否有任何HTML节点id=breakf.如果没有,您应该正确地收到该错误.
| 归档时间: |
|
| 查看次数: |
18392 次 |
| 最近记录: |