gpt4 book ai didi

javascript - 使用 JavaScript 和 PhoneGap 循环 HTML 表

转载 作者:行者123 更新时间:2023-12-02 19:21:41 25 4
gpt4 key购买 nike

我正在使用 android 的phonegap 删除一个应用程序,并且我正在尝试使用 html 表格行在 javascript 中创建一个 FOR 循环。我尝试使用 document.write 但页面中的所有内容都消失了,仅显示 document.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>

谢谢。

最佳答案

这是因为您只是将文本放入页面中,所以需要“创建”元素并将它们附加到表格中。你可以这样做:

<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>

insertRow() 和 insertCell() 可能也可以工作,但我还没有测试它

关于javascript - 使用 JavaScript 和 PhoneGap 循环 HTML 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12478033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com