ToohoKing
2015-03-04 07:55
<!DOCTYPE html>
<html>
<head>
<title> new document </title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk"/>
<script type="text/javascript">
window.onload = function(){
// 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色。
var tr=document.getElementsByTagName("tr");
for(var i=0;i<tr.length;i++){
tr[i].index = i; //给当前对象添加属性记录索引值
tr[i].onmouseover=function(){
tr[this.index].style.backgroundColor="#f2f2f2";
}
tr[i].onmouseout=function(){
tr[this.index].style.backgroundColor="#fff";
}
}
}
// 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色
// 编写一个函数,供添加按钮调用,动态在表格的最后一行添加子节点;
function addrow(sno,name){
var newrow = document.createElement('tr');
var tab = document.getElementById('table');
tab.appendChild(newrow);
var newtd = [];
for(var i=0;i<3;i++){
newtd[i] = document.createElement('td');
newrow.appendChild(newtd[i]);
}
newtd[0].innerHTML = sno;
newtd[1].innerHTML = name;
newtd[2].innerHTML = "<a href='javascript:;' onclick='delrow(this)'>删除</a>";
}
// 创建删除函数
function delrow(x){
var par = x.parentNode.parentNode;
par.parentNode.removeChild(par);
}
</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='delrow(this)' >删除</a></td> <!--在删除按钮上添加点击事件 -->
</tr>
<tr>
<td>xh002</td>
<td>刘小芳</td>
<td><a href="javascript:;" onclick='delrow(this)' >删除</a></td> <!--在删除按钮上添加点击事件 -->
</tr>
</table>
<input type="button" value="添加一行" onclick = "addrow('xh003','张小三')" /> <!--在添加按钮上添加点击事件 -->
</body>
</html>mark 回头再看看
德玛西亚!!!
JavaScript进阶篇
469062 学习 · 22582 问题
相似问题