<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
*{
margin: 0;
padding: 0;
}
.box{
width: 200px;
height: 200px;
background-color: plum;
line-height: 200px;
text-align: center;
margin: 100px auto;
color: #ffffff;
cursor: pointer;
}
td{
width: 200px;
text-align: center;
height: 30px;
}
th{
background-color: palevioletred;
width: 200px;
height: 30px;
}
td,th{
border: 1px solid #ffb0b0;
}
.btn{
border: none;
width: 40%;
background-color: rgb(231, 57, 26);
color: #ffffff;
height: 25px;
border-radius: 5px;
cursor: pointer;
}
.table-box{
margin: 20px auto;
width: 810px;
}
table{
border-collapse: collapse;/*关键代码*/
}
</style>
<body>
<div class="table-box">
<table>
<thead>
<tr>
<th>name</th>
<th>sex</th>
<th>age</th>
<th>操作</th>
</tr>
</thead>
<tbody id="conteiner"></tbody>
</table>
</div>
</body>
<script>
window.onload = function(){
var list = [ {name:"张飞",sex:"男",age:'20',btn:"删除"},
{name:"刘备",sex:"男",age:'20',btn:"删除"},
{name:"关羽",sex:"男",age:'20',btn:"删除"},
{name:"马超",sex:"男",age:'20',btn:"删除"},
{name:"赵云",sex:"男",age:'20',btn:"删除"},
{name:"黄忠",sex:"男",age:'20',btn:"删除"}
]
var tbodydom = document.getElementById('conteiner')
function render(list){
for(var i = 0 ; i < list.length; i ++){
var trbox = document.createElement('tr')
var chlid = list[i]
for(var key in chlid ){
var tdDom = document.createElement('td')
tdDom.innerText = chlid[key]
trbox.appendChild(tdDom)
tbodydom.appendChild(trbox)
}
}
}
render(list)
}
</script>
</html>