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

为什么我的函数changecolor()和del(i)在调用的时候出问题了呢?没反应,求指出错误的地方

为什么我的函数changecolor()和del(i)在调用的时候出问题了呢?没反应,求指出错误的地方

woooopan 2016-04-14 21:25:58
<!DOCTYPE html> <html>  <head>   <title> new document </title>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>      <script type="text/javascript">        window.onload = function(){        changecolor()        }               function changecolor(){        var mytr=document.getElementsByTagName("tr");        for (i=mytr.length-1;i>-1;i--){        mytr[i].setAttribute("onmouseover","color1()")           mytr[i].setAttribute("onmouseout","color2()")           }           }           }           function color1(){           mytr[i].style.backgroundColor="#f2f2f2";}           function color2(){           mytr[i].style.backgroundColor="#fff";}                       // 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色。         function addnode(){          var tr1=document.createElement("tr");          var td1=document.createElement("td");          var td2=document.createElement("td");          var td3=document.createElement("td");          td1.innerHTML="xs003";          var table=document.getElementById("table");          table.appendChild(tr1);          tr1.appendChild(td1);          tr1.appendChild(td2);          tr1.appendChild(td3);         }              // 编写一个函数,供添加按钮调用,动态在表格的最后一行添加子节点;      function del(i){       var x=document.getElementById("table");       x.removeChild(x.nodeChilds[i])      }            // 创建删除函数   </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="del(1)">删除</a></td>   <!--在删除按钮上添加点击事件  -->    </tr>    <tr> <td>xh002</td> <td>刘小芳</td> <td><a href="javascript:del(2);" >删除</a></td>   <!--在删除按钮上添加点击事件  -->    </tr>      </table>    <input type="button" value="添加一行"  onclick="addnode()" />   <!--在添加按钮上添加点击事件  -->  </body> </html>
查看完整描述

3 回答

?
可爱的龟龟

TA贡献18条经验 获得超5个赞

 function color1(){

                   mytr[i].style.backgroundColor="#f2f2f2";}

           function color2(){

               mytr[i].style.backgroundColor="#fff";}

这里 mytr[i]都不在一个作用域,,,

可以改成

 function color1(){

                   this.style.backgroundColor="#f2f2f2";}


查看完整回答
反对 回复 2016-04-14
  • 3 回答
  • 0 关注
  • 1990 浏览
慕课专栏
更多

添加回答

举报

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