原生的js实现table

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>原生js做table练习(runoob.com)</title>
<script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
</head>
<body>

<table id='mytable'>
  <tr>
  	<th>姓名</th>
  	<th>年龄</th> 
  </tr>
</table>
</body>
</html>

<script>
window.onload=function(){
  var table1 = document.getElementById('mytable');
var arr = [
  {
  name:"张三",
  age:12
},
    {
  name:"李四",
  age:34
}
]
var test = ""
for(let i=0; i < arr.length; i++){
test += "<tr>"
  let obj = arr[i]
  console.log(obj)
  let keys = Object.keys(obj)
  	for(let j =0; j<keys.length;j++){
      test += "<td>"+ obj[keys[j]] + "</td>"
    }
  test += "</tr>" 
}
table1.innerHTML += test
}
</script>

相关推荐