当前位置:   article > 正文

使用div制作表格效果_div网页数据表格

div网页数据表格

1 基础版展示

在这里插入图片描述
点击前往在线展示

1.1代码

1.2HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用div制作表格</title>
</head>
<body>
    <div class="mytable1">
    <div class="myTr1">
        <div>11</div>
        <div>12</div>
        <div>13</div>
        <div class="mytdRight">14</div>
    </div>
    <div class="myTr1">
        <div>21</div>
        <div>22</div>
        <div>23</div>
        <div class="mytdRight">24</div>
    </div>
    <div class="myTr1">
        <div>31</div>
        <div>32</div>
        <div>33</div>
        <div class="mytdRight">34</div>
    </div>
    <div class="myTr1">
        <div class="myButtom">41</div>
        <div class="myButtom">42</div>
        <div class="myButtom">43</div>
        <div class="mytdRight myButtom">44</div>
    </div>
    
    </div>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

1.3CSS

 .mytable1{
            width: 95%;
            margin: 0 auto;
            text-align: center;
            
        }
        .myTr1{
            width: 100%;
            height: 30px;
            background-color: rgba(26, 236, 15, 0.587);
            margin: 0 auto;
            display: flex;
        }
        .myTr1 div{
            flex: 1;
            height: 100%;
            background-color: rgba(11, 166, 232, 0.55);
            border-top: 1px red solid;
            border-left: 1px red solid;
        }
        .mytdRight{
            border-right: 1px red solid;

        } 
        .myButtom{
            border-bottom: 1px red solid;

        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

2进阶版展示(动态生成表格)

在这里插入图片描述
点击前往在线展示

2.1代码

2.2HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动态生成表格</title>
</head>
<body>
    <input type="button" value="生成" onclick="getTable()">
    <div class="table" >
    </div>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2.3CSS

.table{
            width: 95%;
            background-color: rgb(11, 224, 244);
            margin: 0 auto;
            text-align: center;
        }
        .table .th{
            width: 100%;
            height: 30px;
            background-color: rgba(26, 236, 15, 0.587);
            margin: 0 auto;
            display: flex;
        }
        .table .th div{
            flex: 1;
            height: 100%;
            background-color: rgba(11, 166, 232, 0.55);
            border-top: 1px red solid;
            border-left: 1px red solid;
        }
        .tableButtom{
            border-bottom: 1px red solid;
        }
        .tableRight{
            border-right: 1px red solid;
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

2.4JS

        function getTable(){
            let Table = document.querySelector(".table")
            Table.innerHTML = ""
             //假设有一个从后台传过来的数据串
	        let data= "001,张三五,1.79,女,团员|002,杨毅,1.72,男,党员|003,杨春三,1.62,男,党员|004,张明明,1.72,女,团员";
            let tr = data.split("|")
            let str = ""
            for(let i=0;i<tr.length;i++){
                str += "<div class='th'>"
                    let td = tr[i].split(",")
                    for(let j=0;j<td.length;j++){
                        
                        if(i==tr.length-1 && j==td.length-1){
                            str += "<div class='tableButtom tableRight'>"+td[j]+"</div>"
                            continue
                        }
                        if(i==tr.length-1){
                            str += "<div class='tableButtom'>"+td[j]+"</div>"
                            continue
                        }
                        if(j==td.length-1){
                            str += "<div class='tableRight'>"+td[j]+"</div>"
                            continue
                        }
                        str += "<div>"+td[j]+"</div>"
                    }
                str += "</div>"

            }
            Table.innerHTML = str
            console.log(str)
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

2.5完整版

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动态生成表格</title>
    <style>
        .table{
            width: 95%;
            background-color: rgb(11, 224, 244);
            margin: 0 auto;
            text-align: center;
        }
        .table .th{
            width: 100%;
            height: 30px;
            background-color: rgba(26, 236, 15, 0.587);
            margin: 0 auto;
            display: flex;
        }
        .table .th div{
            flex: 1;
            height: 100%;
            background-color: rgba(11, 166, 232, 0.55);
            border-top: 1px red solid;
            border-left: 1px red solid;
        }
        .tableButtom{
            border-bottom: 1px red solid;
        }
        .tableRight{
            border-right: 1px red solid;
        }
    </style>
</head>
<body>
    <input type="button" value="生成" onclick="getTable()">
    <div class="table" >
    </div>
    <script>
        function getTable(){
            let Table = document.querySelector(".table")
            Table.innerHTML = ""
             //假设有一个从后台传过来的数据串
	        let data= "001,张三五,1.79,女,团员|002,杨毅,1.72,男,党员|003,杨春三,1.62,男,党员|004,张明明,1.72,女,团员";
            let tr = data.split("|")
            let str = ""
            for(let i=0;i<tr.length;i++){
                str += "<div class='th'>"
                    let td = tr[i].split(",")
                    for(let j=0;j<td.length;j++){
                        
                        if(i==tr.length-1 && j==td.length-1){
                            str += "<div class='tableButtom tableRight'>"+td[j]+"</div>"
                            continue
                        }
                        if(i==tr.length-1){
                            str += "<div class='tableButtom'>"+td[j]+"</div>"
                            continue
                        }
                        if(j==td.length-1){
                            str += "<div class='tableRight'>"+td[j]+"</div>"
                            continue
                        }
                        str += "<div>"+td[j]+"</div>"
                    }
                str += "</div>"

            }
            Table.innerHTML = str
            console.log(str)
        }

    </script>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/767464
推荐阅读
相关标签
  

闽ICP备14008679号