【发布时间】:2018-04-10 22:56:15
【问题描述】:
我是 node.js 的新手。这是我的代码:
My Code
更具体地说,这是我对 JS 的看法:
let torontoTeams = [
{"name": "Raptors", "description": "The Raptors compete in the National Basketball Association (NBA), as a member club of the league's Eastern Conference Atlantic Division."},
{"name": "Maple Leafs", "description": "The Maple Leafs are one of the 'Original Six' NHL teams, and have won the Stanley Cup 13 times."},
{"name": "Blue Jays", "description": "The Blue Jays compete in Major League Baseball (MLB) as a member club of the American League (AL) East division."}
];
for (let i=0; i < torontoTeams.length; i++) {
let newSection = document.createElement('section');
document.appendChild(newSection);
let newTeam = document.createElement('h1');
newTeam.appendChild(document.createTextNode(torontoTeams[i].name));
let newDesc = document.createElement('P');
newDesc.appendChild(document.createTextNode(torontoTeams[i].description));
document.createElement(newSection);
newSection.appendChild(newTeam);
newSection.appendChild(newDesc);
};
我不确定我在创建 HTML 元素时哪里出错了。
【问题讨论】:
-
这是错误的:
document.createElement(newSection);.createElement()将 tagName 作为参数,而不是 DOM 元素,并且您希望将部分放入document.body,而不是document。 -
“我是 node.js 的新手” - Node.js 在服务器上运行 JavaScript,在这里创建 DOM 元素没有意义。显示的代码是否应该在浏览器而不是 Node.js 中运行?
-
是的,它应该在浏览器中运行,但我想在这种情况下根本不需要 node.js?
标签: javascript html arrays node.js