<html>
<head>
<title> new document </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
window.onload = function(){
// 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色。
//获取每一行
var line = document.getElementsByTagName("tr");
for(var i = 0;i < line.length ;i++)
{
line[i].onmouseover = function()
{
// alert("测试下鼠标滑过这行有动静吗?");
line[i].style.backgroundColor = red;
}
line[i].onmouseout = function()
{
// alert("测试过鼠标滑出时候的动静");
line[i].style.backgroundColor = yellow;
}
}
}
// 编写一个函数,供添加按钮调用,动态在表格的最后一行添加子节点;
function addLine(){
// alert("测试方法是否调用,果然还是管用的方法");
}
// 创建删除函数
function remo(){
//获取所在行
//删除所在行
// alert("测试方法是否调用,果然还是管用的方法");
}
</script>
</head>
<body>
<table border="7" width="50%" id="table">
<tr>
<th>学号</th>
<th>姓名</th>
<th>操作</th>
</tr>
<tr>
<td>xh001</td>
<td>王小明</td>
<td><a href="javascript:remo();" >删除</a></td> <!--在删除按钮上添加点击事件 -->
</tr>
<tr>
<td>xh002</td>
<td>刘小芳</td>
<td><a href="javascript:remo();" >删除</a></td> <!--在删除按钮上添加点击事件 -->
</tr>
</table>
<input type="button" value="添加一行" onclick ="addLine()" /> <!--在添加按钮上添加点击事件 -->
</body>
</html>问题:19行和24行的 颜色设置不成功.希望实现的是:当鼠标滑进每一行的时候该行背景颜色改变,滑出这一行的时候背景颜色也改变.
5 回答
已采纳
![?](http://img1.sycdn.imooc.com/545863aa00014aa802200220-100-100.jpg)
刚毅87
TA贡献345条经验 获得超309个赞
总共有两处错误
鼠标事件是在 for循环走完之后再执行,所以此时 i= line.length. 并不存在,所以用 this 代指 line[i].
颜色要用引号括起来
代码如下
<html> <head> <title> new document </title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript"> window.onload = function(){ // 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色。 //获取每一行 var line = document.getElementsByTagName("tr"); for(var i = 0;i < line.length ;i++) { line[i].onmouseover = function() { // alert("测试下鼠标滑过这行有动静吗?"); this.style.backgroundColor = 'red'; } line[i].onmouseout = function() { // alert("测试过鼠标滑出时候的动静"); this.style.backgroundColor = 'yellow'; } } } // 编写一个函数,供添加按钮调用,动态在表格的最后一行添加子节点; function addLine(){ // alert("测试方法是否调用,果然还是管用的方法"); } // 创建删除函数 function remo(){ //获取所在行 //删除所在行 // alert("测试方法是否调用,果然还是管用的方法"); } </script> </head> <body> <table border="7" width="50%" id="table"> <tr> <th>学号</th> <th>姓名</th> <th>操作</th> </tr> <tr> <td>xh001</td> <td>王小明</td> <td><a href="javascript:remo();" >删除</a></td> <!--在删除按钮上添加点击事件 --> </tr> <tr> <td>xh002</td> <td>刘小芳</td> <td><a href="javascript:remo();" >删除</a></td> <!--在删除按钮上添加点击事件 --> </tr> </table> <input type="button" value="添加一行" onclick ="addLine()" /> <!--在添加按钮上添加点击事件 --> </body> </html>
望采纳!
- 5 回答
- 1 关注
- 2415 浏览
添加回答
举报
0/150
提交
取消