使用JavaScript和PhoneGap循环HTML表

使用JavaScript和PhoneGap循环HTML表,javascript,android,html,cordova,Javascript,Android,Html,Cordova,我正在用phonegap为android删除一个应用程序,并试图在javascript中用html表行为创建一个循环。我尝试使用文档。write但是页面中的所有内容都显示出来,只显示文档中的内容。write。 我的代码是这样的: <table id="hor-minimalist-b"> <script language=javascript> for (var i=0; i<3; i++) { document.write('<tr>');

我正在用phonegap为android删除一个应用程序,并试图在javascript中用html表行为创建一个循环。我尝试使用
文档。write
但是页面中的所有内容都显示出来,只显示
文档中的内容。write
。 我的代码是这样的:

<table id="hor-minimalist-b">
<script language=javascript>
for (var i=0; i<3; i++) {
    document.write('<tr>');
        document.write('<td>');
        document.write('<input type="text" name="valor" id="valor" value="key' + i'">');
        document.write('</td>');
        document.write('</tr>');
}
</script>
</table>


对于(var i=0;i来说,这是因为您只是在页面中放置文本,所以需要“创建”元素并将它们附加到表中。 您可以这样做:

<table id="hor-minimalist-b"><thead></thead><tbody></tbody></table>
<script language="javascript">
var table = document.getElementById('hor-minimalist-b'); // get the table element
var tableBody = table.childNodes[1]; // get the table body
var tableHead = table.childNodes[0]; // get the table head
for (var i=0; i<3; i++) {
    var row = document.createElement('tr'); // create a new row
    var cell = document.createElement('td'); // create a new cell
    var input = document.createElement('input'); // create a new input
    // Set input properties
    input.type = "text";
    input.id = "valor"; // It's not a good idea (to have elements with the same id..)
    input.name = "valor";
    input.value = "key" + i;
    cell.appendChild(input); // append input to the new cell
    row.appendChild(cell); // append the new cell to the new row
    tableBody.appendChild(row); // append the row to table body
}
</script>

var table=document.getElementById('hor-minimalist-b');//获取表元素
var tableBody=table.childNodes[1];//获取表体
var tableHead=table.childNodes[0];//获取表头

对于(var i=0;我想知道答案。我对javascript有点陌生。我不确定我是否理解:在我的代码中,我应该添加你给我的这个?另一件事,你能解释一下tableBody=table.firstChild的用途是什么吗?谢谢你应该用这个替换你的代码。使用tbody是一个很好的实践,它会让你的code与Internet Explorer兼容。它可以工作,非常感谢:)顺便说一句,我想添加一个theader。你能编辑你用它编写的代码吗?我还在努力理解它。谢谢:)当然,它是经过编辑的,有注释可以帮助你理解它。非常感谢。这真的很有帮助。很抱歉,我一直在问新的问题,但是,如果我有标签,我也必须用document.createElement创建var,不是吗?如果我想把纯文本放在两者之间,我该怎么做?再次感谢