为了账号安全,请及时绑定邮箱和手机立即绑定

我设置的背景颜色为什么不显示呢? ---------js进阶9-22编程练习.

我设置的背景颜色为什么不显示呢? ---------js进阶9-22编程练习.

bestDove 2016-06-18 14:47:50
<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 回答

已采纳
?
刚毅87

TA贡献345条经验 获得超309个赞

总共有两处错误

  1. 鼠标事件是在 for循环走完之后再执行,所以此时 i= line.length. 并不存在,所以用 this 代指 line[i].

  2. 颜色要用引号括起来

代码如下

<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>

望采纳!

查看完整回答
4 反对 回复 2016-06-18
?
K丶L

TA贡献51条经验 获得超21个赞

19行的red和24行的yellow要加引号

"red"         "yellow"

查看完整回答
1 反对 回复 2016-06-18
?
bestDove

TA贡献1条经验 获得超0个赞

this 这个点还是有点模糊 先者样用着把 以后遇到再说

查看完整回答
反对 回复 2016-06-18
  • 5 回答
  • 1 关注
  • 2415 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信