需要制作數字從 1 到 100 的表格,它應該是 10x10 單元格。我制作了 10x10 的單元格,但結果填充了從 1 到 10 的每條網格線,如何使每條線像 1 到 10、11 到 20 和 100 一樣?感謝您關注我的問題。
function generate_table() {
const tbl = document.createElement("table");
const tblBody = document.createElement("tbody");
for (let i = 1; i < 11; i ) {
const row = document.createElement("tr");
for (let j = 0; j < 10; j ) {
const cell = document.createElement("td");
const cellText = document.createTextNode(i);
cell.appendChild(cellText);
row.appendChild(cell);
}
tblBody.appendChild(row);
}
tbl.appendChild(tblBody);
document.body.appendChild(tbl);
tbl.setAttribute("border", "1");
}
<form action="#">
<input type="button" value="Generate a table" onclick="generate_table()">
</form>
uj5u.com熱心網友回復:
function generate_table() {
const tbl = document.createElement("table");
const tblBody = document.createElement("tbody");
for (let y = 0; y < 10; y ) {
const row = document.createElement("tr");
for (let x = 0; x < 10; x ) {
const cell = document.createElement("td");
const cellText = document.createTextNode((x 10 * y) 1);
cell.appendChild(cellText);
row.appendChild(cell);
}
tblBody.appendChild(row);
}
tbl.appendChild(tblBody);
document.body.appendChild(tbl);
tbl.setAttribute("border", "1");
}
<form action="#">
<input type="button" value="Generate a table" onclick="generate_table()">
</form>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/495794.html
標籤:javascript html
上一篇:Javascript改進折疊元素
下一篇:阻止訪問小螢屏上的頁面
