生成动态5行3列的表格,带表头列头,随机数15个
时间: 2024-01-04 11:02:25 浏览: 26
好的,这里提供一份 HTML 和 JavaScript 代码,可以生成一个 5 行 3 列的表格,每个单元格中填充一个随机整数。
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Dynamic Table</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 5px;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th></th>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
</tr>
</thead>
<tbody>
<script>
const rows = 5;
const cols = 3;
for (let i = 0; i < rows; i++) {
const row = document.createElement("tr");
const header = document.createElement("td");
header.innerText = `Row${i+1}`;
row.appendChild(header);
for (let j = 0; j < cols; j++) {
const cell = document.createElement("td");
cell.innerText = Math.floor(Math.random() * 100);
row.appendChild(cell);
}
document.querySelector("tbody").appendChild(row);
}
</script>
</tbody>
</table>
</body>
</html>
```
这段代码会生成一个带表头和列头的表格,表格中共有 5 行和 3 列,每个单元格中填充一个 0-99 的随机整数。你可以根据需要修改行数、列数、表头和列头的内容和样式。