<input type="button" value="创建">
<style>*{margin: 0;padding: 0;}table{width: 980px;margin: 50px auto;}table,th,tr,td{text-align: center;border: 1px solid #ccc;}</style>
<script>var heads = ['姓名', '年龄', '性别', '学号', '薪资', '城市', '操作'];var datas = [{name:'欧阳霸天',age:19,gender:'男',stuId:'1001',salary:'20000',city:'上海'},{name:'令狐霸天',age:19,gender:'男',stuId:'1001',salary:'20000',city:'北京'},{name:'欧阳霸天',age:19,gender:'男',stuId:'1001',salary:'20000',city:'上海'},{name:'令狐霸天',age:19,gender:'男',stuId:'1001',salary:'20000',city:'北京'},];var btn = document.querySelector('input');btn.onclick = function(){var table = document.createElement('table');var fst_tr = document.createElement('tr');for (let i = 0; i < heads.length; i++) {var th = document.createElement('th');th.innerText = heads[i];fst_tr.appendChild(th);}table.appendChild(fst_tr);for (let i = 0; i < datas.length; i++) {var tr = document.createElement('tr');for (key in datas[i]) {var td = document.createElement('td');td.innerText = datas[i][key];tr.appendChild(td);}var last_td = document.createElement('td')last_td.innerHTML = ' <a href="javascript:;">删除</a>';tr.appendChild(last_td);table.appendChild(tr);}document.body.appendChild(table);var trs = document.querySelectorAll('tr:nth-child(n+2)')for (let i = 0; i < trs.length; i++) {trs[i].onmouseover = function(){this.style.background = 'pink';}trs[i].onmouseout = function(){this.style.background = '';}}var alinks = document.querySelectorAll('a');for (let i = 0; i < alinks.length; i++) {alinks[i].onclick = function(){if(confirm('确定要删除吗?')){table.removeChild(this.parentNode.parentNode);}else{alert('取消删除'); }}}}</script>
实现效果: