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

<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行的 颜色设置不成功.希望实现的是:当鼠标滑进每一行的时候该行背景颜色改变,滑出这一行的时候背景颜色也改变.

bestDove
浏览 2416回答 5
5回答

刚毅87

总共有两处错误鼠标事件是在 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>望采纳!

K丶L

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

bestDove

this 这个点还是有点模糊 先者样用着把 以后遇到再说
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5