2017-07-21 14 views
1

は私のコードです:ここではこのコードがなぜ機能しないのですか?ここ

  document.getElementById("charthelp").innerHTML+="<table>"; 
 
      for(var i=0; i<data.ertekek.length; i++) 
 
      { 
 
       szinek = [Math.floor((Math.random() * 200) + 1), Math.floor((Math.random() * 200) + 1), Math.floor((Math.random() * 200) + 1)]; 
 
       charts[i]=new TimeSeries(); 
 
       smoothie.addTimeSeries(charts[i], { strokeStyle: 'rgb('+szinek[0]+', '+szinek[1]+', '+szinek[2]+')', fillStyle: 'rgba(0, 255, 0, 0.0)', lineWidth: 3 }); 
 
       if(i%2==1){ document.getElementById("charthelp").innerHTML+= '<tr><td><input type="checkbox" id="'+i+'", onclick="handleClick(this)"><span style="background-color:rgb('+szinek[0]+', '+szinek[1]+', '+szinek[2]+')">&nbsp &nbsp &nbsp</span>'+data.ertekek[i].neve+'</input></td>'; } 
 
       else { document.getElementById("charthelp").innerHTML+= '<td><input type="checkbox" id="'+i+'", onclick="handleClick(this)"><span style="background-color:rgb('+szinek[0]+', '+szinek[1]+', '+szinek[2]+')">&nbsp &nbsp &nbsp</span>'+data.ertekek[i].neve+'</input></td></tr>'; } 
 
       inic=true; 
 
      } 
 
      document.getElementById("charthelp").innerHTML+='</table>';

は、レンダリングされたHTMLコードです:

<div id="charthelp"><input type="checkbox" id="0" ,="" onclick="handleClick(this)"><table> 
 
    
 
</table> 
 
<input type="checkbox" id="1" ,="" onclick="handleClick(this)"><span style="background-color:rgb(109, 12, 20)">&nbsp; &nbsp; &nbsp;</span>Elektronikai csarnok 
 
<input type="checkbox" id="2" ,="" onclick="handleClick(this)"><span style="background-color:rgb(92, 148, 103)">&nbsp; &nbsp; &nbsp;</span>Bemutatóterem 
 
<input type="checkbox" id="3" ,="" onclick="handleClick(this)"><span style="background-color:rgb(121, 81, 16)">&nbsp; &nbsp; &nbsp;</span>Fejlesztés

そして、ここでは、テーブルの写真です:Picture

ご覧のとおり、JSコードは機能しません。それは順番に行うべきですが、最初と最後の行が最初に実行されます。その後、繰り返しが実行されます。私はそれがテーブルでなければならないと思うが、テーブルではない。

+0

を使用してみてくださいは、変数として、あなたのHTML文字列を構築するのではなく、直接たびに、innerHTMLプロパティを設定してみてください。プロセスの最後に、作成した文字列を適用して、innerHTMLコマンドを一度実行してください。 – ADyson

答えて

3

<table>をHTMLに追加しようとすると、ブラウザに自動的に</table>が追加されると思う。

var table = document.createElement('table'); 
var tr = document.createElement('tr'); 
table.appendChild(tr); 
... 
document.getElementById("charthelp").appendChild(table); 
関連する問題