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.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;
}
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>
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;
}
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)
}
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>