不能眼高手低啊
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<script type="text/javascript">
window.onload = function(){
// 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色。
changeColor();
}
function changeColor(){
var trs = document.getElementsByTagName("tr");
for(let i=1;i<trs.length;i++){
trs[i].onmouseover = function(){
this.style.background = "#ADD8E6";
};
trs[i].onmouseout = function(){
this.style.background = "";
}
}
}
// 编写一个函数,供添加按钮调用,动态在表格的最后一行添加子节点;
function add(){
var num = prompt("请输入学号","xh");
var name = prompt("请输入姓名","xxx");
var newTr = document.createElement("tr");
var td1 = document.createElement("td");
td1.innerHTML = num;
var td2 = document.createElement("td");
td2.innerHTML = name;
var td3 = document.createElement('td');
var a = document.createElement("a");
a.setAttribute("href","javascript:;");
a.setAttribute("onclick","deleterow(this)");
a.innerHTML = "删除";
td3.appendChild(a);
newTr.appendChild(td1);
newTr.appendChild(td2);
newTr.appendChild(td3);
var table = document.getElementById("table");
table.appendChild(newTr);
changeColor();
}
// 创建删除函数
function deleterow(obj){
obj.parentNode.parentNode.parentNode.removeChild(obj.parentNode.parentNode);
}
</script>
<body>
<table border="1" width="50%" id="table">
<tr>
<th>学号</th>
<th>姓名</th>
<th>操作</th>
</tr>
<tr>
<td>xh001</td>
<td>王小明</td>
<td><a href="javascript:;" onclick="deleterow(this)">删除</a></td> <!--在删除按钮上添加点击事件 -->
</tr>
<tr>
<td>xh002</td>
<td>刘小芳</td>
<td><a href="javascript:;" onclick="deleterow(this)">删除</a></td> <!--在删除按钮上添加点击事件 -->
</tr>
</table>
<input type="button" value="添加一行" onclick="add()" /> <!--在添加按钮上添加点击事件 -->
</body>
</html>