2012-03-13 6 views
0

"メソッド 'appendchild'をヌルにコールすることはできません" ...エラー...なぜですか?ここ

var main = document.getElementById("section"); //gets node for main window 
     var para1 = document.createElement("p");  //creates paragraph  node 
     var para2 = document.createElement("p"); // creates another paragraph node 

     var req = request.responseXML; //gets xml data 

     var nodeList = req.getElementsByTagName("line"); //gets all nodes with line as tag 
     var nodeArray = new Array(); //holds only lines that are part of code 

     para1.appendChild(document.createTextNode(req.getElementsByTagName("description")[0].childNodes[0].nodeValue)); //creates text node to put inside paragraph node 
     para2.appendChild(document.createElement("table")); //creates a table tag and puts inside para2 

     for(var i=0; i<nodeList.length; i++) 
     { 
      if(nodeList[i].getAttribute("st") == "no") 
      { 
       document.getElementById("table").appendChild(document.createElement("tr")); //creates a table row for each line participating in quiz 
       //document.getElementById("tr").appendChild(document.createElement("td")); //creates table data for the row (only one)dd 
       //nodeArray.push(nodeList[i].childNodes[0].nodeValue); //adds to array the lines participating in quiz   
       //document.getElementById("td").appendChild(createTextNode(nodeList[i],childNodes[0].nodeValue)); 
      } 
     } 


     main.appendChild(para1); //add paragraph to the main div 
     main.appendChild(para2); //adds 2nd paragraph to main div 
    } 

は、だから私はif文で他のすべてをコメントアウトしているが、私はまだ、このエラーを取得しています...私はすでに以前のテーブルタグを作成した場合、それはなぜ...私のコードですヌルと見なされる

+0

IE <9でエラーが発生するため、TR要素をTABLE要素に追加しないでください。これらをTBODY要素に追加する必要があります。他のブラウザでは、TBODYが挿入されていない場合、または最後のTBODY要素に自動的に行が追加されます(IEではなく)。 – RobG

答えて

3
にコードを変更し

:今tbodyに行を追加

var table = para2.appendChild(document.createElement("table")); 
var tbody = table.appendChild(document.createElement('tbody')); 

。他の答えはgetElementByIdエラーについて教えてください。あなたの質問に対するコメントは、TBODY要素を作成する理由を示しています。

+0

はい、それをやった!本当にありがとう – carinlynchin

0

"table"というIDをテーブルに割り当てていないため、getElementById( "table")で見つからないでしょう。

テーブルに適切なIDを割り当てるか、getElementsByTagNameを使用します。

+0

これを変更しましたが、このエラーが発生しました。Uncaught TypeError:Object#に 'appendChild'メソッドがありません – carinlynchin

+0

getElementsByTagNameは、getElementByIdのような単一要素ではなく、NodeListを返します。 appendChildは要素上で使用できますが、NodeListでは使用できません。 – Steve

0
para2.appendChild(document.createElement("table")); 

ブロック、getElementsByName

0

を使用してアクセスそれはあなたがappendChildには、いくつかの呼び出しを持っているので、それが難しい場合、これはあなたに

を名前の要素「テーブル」ではなく、ID「表」を作成します'table'行でエラーが発生していることを検証します。しかし、それは言った、あなたは、ID tableを持つテーブルを作成するために必要なことを意味するdocument.getElementsByTagNamedocument.getElementByIdをしませ呼んでいる:

<table id="table"> 
... 
</table> 
関連する問題