使用js数组渲染学生信息

将学生信息写入js数组,再使用for循环遍历渲染到页面。

效果图:

全代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=\, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    table{
        width: 600px;
        text-align: center;
    }
    table,th,td{
        border: 1px solid#ccc;
        border-collapse: collapse;
    }
    caption{
        margin-bottom: 20px;
        font-size: 20px;
    }
    tr{
        height: 40px;
        cursor: pointer;
    }
    table tr:nth-child(1){
        background-color: #ddd;
    }
    table td:not(:first-child):hover{
        background-color: #eee;
    }
</style>
<body>
    <h2>学生信息</h2>
    <table>
        <caption>学生列表</caption>
        <tr>
            <td>序号</td>
            <td>姓名</td>
            <td>年龄</td>
            <td>性别</td>
            <td>家乡</td>
        </tr>
       <script>
        let student = [
            {name:'小刚',age:18,gender:'男',hometown:'湖北'},
            {name:'小红',age:19,gender:'女',hometown:'东北'},
            {name:'小明',age:18,gender:'男',hometown:'湖北'},
            {name:'小原',age:21,gender:'男',hometown:'安溪'},
            {name:'那由多',age:16,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>
           ` )
        }
       </script>
    </table>
</body>
</html>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值