CSS部分代码:
<style>
* {
text-align: center;
padding: 0px;
margin: 0px;
}
h2 {
margin-top: 100px;
}
table {
border: 1px solid gray;
text-align: center;
margin-left: 35%;
}
th {
background-color: #eee;
padding: 0px 30px;
}
/* 除了第一行的tr,其余放上去变为小手 */
table tr:not(:first-child):hover {
background-color: #eee;
cursor: pointer;
}
</style>
JS部分代码:
<script>
document.write(`
<h2>学生信息</h2>
<table>
<caption>学生列表</caption>
<tr>
<th>序号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>家乡</th>
</tr>
`)
let student = [
{ name: '小明', age: '18', gender: '男', hometown: '河北省' },
{ name: '小红', age: '17', gender: '女', hometown: '河南省' },
{ name: '小刚', age: '19', gender: '男', hometown: '山西省' },
{ name: '小丽', age: '18', gender: '女', hometown: '山东省' },
]
for (let i = 0; i < student.length; i++) {
//获取到数组里的元素
document.write(`
<tr>
<td>${i + 1}</td>
<td>${student[i].name}</td>
<td>${student[i].age}</td>
<td>${student[i].gender}</td>
<td>${student[i].hometown}</td>
</tr>
`)
}
document.write(`</table>`)
</script>
效果如下,可在数组中新增元素