qq_獨捔嘼_0
2017-03-14 13:30
<!DOCTYPE html>
<html>
<head>
<title> new document </title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk"/>
<style type="text/css">
td{text-align: center;}
</style>
<script type="text/javascript">
window.onload = function(){
td=document.getElementsByTagName("td");
for(var i=0;i<td.length;i++){
td[i].onmouseover=function(){
this.style.backgroundColor="red";
};
td[i].onmouseout=function(){
this.style.backgroundColor="white";
}
}}
var num=2;
function add(){
num++;
tr=document.createElement("tr");
td1=document.createElement("td");
td2=document.createElement("td");
td3=document.createElement("td");
td1.innerHTML="xh00"+num;
td2.innerHTML="第"+num+"学生";
td3.innerHTML="<a href='javascript:;' onclick='del(this)' >删除</a>";
tab=document.getElementById("table");
tab.appendChild(tr);
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
var tr = document.getElementsByTagName("tr");
td=document.getElementsByTagName("td");
for(var i=0;i<td.length;i++){
td[i].onmouseover=function(){
this.style.backgroundColor="red";
};
td[i].onmouseout=function(){
this.style.backgroundColor="white";
}
}
}
function del(obj){
tr=obj.parentNode.parentNode;
table=document.getElementById("table");
table.removeChild(tr);
}
</script>
</head>
<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="del(this)">删除</a></td> <!--在删除按钮上添加点击事件 -->
</tr>
<tr>
<td>xh002</td>
<td>刘小芳</td>
<td><a href="javascript:;" onClick="del(this)">删除</a></td> <!--在删除按钮上添加点击事件 -->
</tr>
</table>
<input type="button" value="添加一行" onClick="add()" /> <!--在添加按钮上添加点击事件 -->
</body>
</html>
tab = document.getElementById("table").lastChild;改为tab =document.getElementById("table").lastChild;
table=document.getElementById("table");改为table = document.getElementById("table").lastChild;
求采纳,谢谢
JavaScript进阶篇
468191 学习 · 21891 问题
相似问题