<!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> *{ margin: 0px; padding: 0px; } table{ width: 500px; margin: 100px auto; border-collapse: collapse; text-align: center; } th,td{ border: 1px solid #333; } thead tr{ height: 30px; background-color: #ccc; } </style> </head> <body> <table> <thead> <tr> <th>姓名</th> <th>科目</th> <th>成绩</th> <th>操作</th> </tr> </thead> <tbody> </tbody> </table> <script> //准备好的数据 var datas = [{ name: ‘魏璎珞‘, subject: ‘JavaScript‘, score: 100 }, { name: ‘弘历‘, subject: ‘JavaScript‘, score: 98 }, { name: ‘傅恒‘, subject: ‘JavaScript‘, score: 99 }, { name: ‘明玉‘, subject: ‘JavaScript‘, score: 88 }, { name: ‘大猪蹄子‘, subject: ‘JavaScript‘, score: 0 }]; var tbody = document.querySelector(‘tbody‘); for(var i=0; i<datas.length; i++){ var tr_ele = document.createElement(‘tr‘); tbody.appendChild(tr_ele); //遍历对象 获取属性名 ( j 表示属性名 ) for(var j in datas[i]){ var td_ele = document.createElement(‘td‘); td_ele.innerHTML = datas[i][j]; tr_ele.appendChild(td_ele); } //创建有删除的单元格 var sc_ele = document.createElement(‘td‘); sc_ele.innerHTML = "<a href=‘javascript:;‘>删除</a>"; tr_ele.appendChild(sc_ele); } //删除操作 var as = document.querySelectorAll(‘a‘); for(var i=0; i<as.length; i++){ as[i].onclick = function(){ tbody.removeChild(this.parentNode.parentNode); } } </script> </body> </html>