3 回答

TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊
function createTable(tableData) {
var table = document.createElement('table')
, tableBody = document.createElement('tbody');
tableData.forEach(function(rowData) {
var row = document.createElement('tr');
rowData.forEach(function(cellData) {
var cell = document.createElement('td');
cell.appendChild(document.createTextNode(cellData));
row.appendChild(cell);
});
tableBody.appendChild(row);
});
table.appendChild(tableBody);
document.body.appendChild(table);
}
createTable([["Manchester City", 92], ["Liverpool", 82], ["Tottenham", 78], ["Chelsea", 78], ["Manchester United", 72], ["Arsenal", 69]]
);

TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個(gè)贊
遍歷您的數(shù)組,對(duì)于每個(gè)頂級(jí)元素,向表中添加一行。然后,對(duì)于每個(gè)下一級(jí)元素,添加一個(gè)單元格并將元素的內(nèi)容插入到表格單元格中。

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超4個(gè)贊
你可以這樣做:
const toTable = (arr) =>
`<table id="codexpl">
<tr>
<th>No</th>
<th>Club</th>
<th>Points</th>
</tr>
${arr.map(
(item, index) =>
`<tr>
<td>${index + 1}</td>
<td>${item[0]}</td>
<td>${item[1]}</td>
</tr>`
).join('\n')}
</table>`
只需將數(shù)組傳遞給它,它就會(huì)返回 HTML 代碼。
添加回答
舉報(bào)