2017-10-30 8 views
1

私はnode.jsを使い慣れましたここに私のコードは次のとおりです。
My CodeJSでHTML要素を作成する(jQueryなし)私は間違って何をしていますか?

具体的には、ここで私は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要素を作成すると間違って行くよどこか分かりません。

+0

でそれをターゲット引数はDOM要素ではなく、 'document'ではなく' document.body'にセクションを入れたいとします。 – jfriend00

+0

* "私はnode.jsを新しくしました" * - Node.jsはサーバー上でJavaScriptを実行しますが、DOM要素の作成には意味がありません。コードはNode.jsではなくブラウザで実行されるはずですか? – nnnnnn

+0

はい、ブラウザで実行するはずですが、この場合はnode.jsは必要ないと思いますか? – daezilla

答えて

1

documentには1つの要素しか追加できません。 ; `` .createElement()は `としてtagNameをかかり`のdocument.createElement(newSection):これは間違っているdocument.bodysection Sを追加またはコンテナを作成し、document.querySelector

var body = document.body, 
 
    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++) { 
 

 
    var newSection = document.createElement('section'); 
 
    body.appendChild(newSection); // append to body. 
 

 
    var newTeam = document.createElement('h1'); 
 
    newTeam.appendChild(document.createTextNode(torontoTeams[i].name)); 
 

 
    var newDesc = document.createElement('P'); 
 
    newDesc.appendChild(document.createTextNode(torontoTeams[i].description)); 
 

 
    // document.createElement(newSection); What is this suppose to do? It doesn't work. 
 
    
 
    newSection.appendChild(newTeam); 
 
    newSection.appendChild(newDesc); 
 
    
 
};
section { 
 
    display: block; 
 
    width: 100vh; 
 
    height: 33vh; 
 
    background-color: blue; 
 
}

+0

ありがとう!ブラウザで動作するように変更する必要があるものはありますか?私のjsファイルに同じ変更を加えると、ここでjsfiddleで実行すると、コードは動作しますが、ブラウザでは動作しません。 – daezilla

+1

@daezillaでは、 'let'を' var'で置き換えます。なぜなら、ほとんどのブラウザは 'let'キーワード(ES6から)をまだサポートしていないからです。 –

+0

varは私のgo-toキーワードですが、この演習では代わりに 'let'を使用するように指示しています。 – daezilla

関連する問題