如何让新添加的行也有跟随鼠标移动的变色效果?
tianjia()函数里的最后一行 q() 为什么调用不了?
代码如下
------------------------------------------------------------------------------------------------------------------------------
<!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 q(){
var tr=document.getElementsByTagName("tr");
for(i=0;i<tr.length;i++)
{
tr[i].style.backgroundColor="grey";
bianse(tr[i])
}
}
function bianse(ojb)
{
ojb.onmouseover=function(){
ojb.style.backgroundColor="pink";
}
ojb.onmouseout=function(){
ojb.style.backgroundColor="grey";
}
}
function tianjia()
{
var tr=document.getElementsByTagName("tr");
var trfather=tr[0].parentNode;
var newtr=document.createElement("tr");
var newtd1=document.createElement("td");
newtd1.innerHTML="xh00"+tr.length;
var newtd2=document.createElement("td");
newtd2.innerHTML="姓名"
var newtd3=document.createElement("td");
var newtd3child=document.createElement("a");
newtd3child.innerHTML="删除";
newtd3child.setAttribute("href","javascript:;");
newtd3child.setAttribute("onclick","shanchu(this)");
newtd3.appendChild(newtd3child);
newtr.appendChild(newtd1);
newtr.appendChild(newtd2);
newtr.appendChild(newtd3);
trfather.appendChild(newtr);
q();
}
function shanchu(ojb)
{
var father=ojb.parentNode.parentNode;
father.parentNode.removeChild(father)
}
</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="shanchu(this)" >删除</a></td> <!--在删除按钮上添加点击事件 -->
</tr>
<tr>
<td>xh002</td>
<td>刘小芳</td>
<td><a href="javascript:;" onclick="shanchu(this)" >删除</a></td> <!--在删除按钮上添加点击事件 -->
</tr>
</table>
<input type="button" value="添加一行" onclick="tianjia()" /> <!--在添加按钮上添加点击事件 -->
</body>
</html>
------------------------------------------------------------------------------------------------------------------------------