<!DOCTYPE html>
<html>
<head>
<title> new document </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
function bgcChange(obj){
obj.onmouseover=function(){
obj.style.backgroundColor="#f2f2f2";
}
obj.onmouseout=function(){
obj.style.backgroundColor="#fff";
}
}
window.onload = function(){//进入页面时,对已经有的td注册鼠标事件。
// 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色。
var ttd=document.getElementsByTagName("td");
for(var i=0;i<ttd.length;i++){//执行完所有循环后,给每一个td注册onmuseover和onmouseout事件;
bgcChange(ttd[i]);
}
}
</script>
<style type="text/css">
td{
width: 30%;
height: 20px;
}
</style>
</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>
</body>
</html> function bgcChange(obj){
obj.onmouseover=function(){
obj.style.backgroundColor="#f2f2f2";
}
obj.onmouseout=function(){
obj.style.backgroundColor="#fff";
}
}
window.onload = function(){
var ttd=document.getElementsByTagName("td");
for(var i=0;i<ttd.length;i++){
bgcChange(ttd[i]);
}
}
其中上面的代码为什么不能简写成下面的:试过了,下面的代码运行达不到想要的效果。
window.onload = function(){
var ttd=document.getElementsByTagName("td");
for(var i=0;i<ttd.length;i++){
ttd[i].onmouseover=function(){
ttd[i].style.backgroundColor="#f2f2f2";
}
ttd[i].onmouseout=function(){
ttd[i].style.backgroundColor="#fff";
}
}
}
tom的猫
相关分类