window.onload = function(){
var tr = document.getElementsByTagName("tr");
for (var i=0; i<tr.length; i++){
change(tr[i]);
}
// 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色
function change(obj){
obj.onmouseover = function(){
obj.style.backgroundColor = "#f2f2f2";
}
obj.onmouseout = function(){
obj.style.backgroundColor = "#fff";
}
}
}
// 编写一个函数,供添加按钮调用,动态在表格的最后一行添加子节点
var num = 2;
function add(){
num++;
var newtr = document.createElement("tr");
var newtd1 = document.createElement("td");
var newtd2 = document.createElement("td");
newtd1.innerHTML = "xh00"+num;
newtd2.innerHTML = "第"+num+"个学生";
var del = document.createElement("td");
del.innerHTML = "<a href='javascript:;' onclick='del(this)' >删除</a>";
var tab = document.getElementById("table");
tab.appendChild(newtr);
newtr.appendChild(newtd1);
newtr.appendChild(newtd2);
newtr.appendChild(del);
var tr = document.getElementsByTagName("tr");
for (var i=0; i<tr.length; i++){
change(tr[i]);
}
}