2017-04-30 11 views
2

大学のコースでJavaScriptを学習する際の問題があります。これは私の最後のプロジェクトであり、HTMLコードで問題が発生しているのは、idのないラベルの入力フィールドを作成することですが、「for」要素もあります。 JQueryやJSONでは作成できません。 ">HTMLを変更せずに「for」属性から入力ラベルを作成

車 < "=のラベル" スパンクラス= "必要 "> <「/ span>を<" /ラベル">どのような車はあなたが持っているん<?":

だからHTMLは次のようになります

だから私は、私は、テキスト入力フィールドを作成し、DOMに追加することができ考えていた:。

// Create the input field 
var firstCar = document.createElement("input"); 
// Create the type node and store what the field will be text 
var newType = document.createTypeNode("text"); 
// Create a new ID of car and attach it to the input 
var newID = document.createIdNode("car") 
/* put the created Element within the HTML with ["this determines which label it will be under"]:*/ 
var position = document.getElementByTagName("label")[1]; 
// Insert the element into the position 
position.appendChild(firstCar); 

私はこれを実行し、それがTypeNodeは私のブラウザでは関数ではありませんと言っている

+0

FYI - 'input'要素が' type'属性を指定していない場合、デフォルトは ''です。 –

+0

ウェブページが変更されましたが、これはもはや要件ではありません。しかし、誰かがこれに答えたいのであれば、DOM操作についてもっと学ぶことに感謝します。 マーカスありがとうございます。 – SikkoB

+0

* getElement ** s ** ByTagName *、複数の要素に注意してください。 – RobG

答えて

3

HOではありませんあなたはtypeidを設定します。これを試してみてください

var firstCar = document.createElement("input"); 

firstCar.type = "text"; // set the type 

firstCar.id = "car"; // set the ID 

var position = document.getElementsByTagName("label")[1]; // it should be getElements... not getElement... 

position.appendChild(firstCar); 
関連する問題