我现在做的一个东西,就是当表格中某行在onmouseover的时候 , background-color改变
html部分代码:
<tr>
<td>June 9th</td>
    <td>Portland,<abbr title="oregen">OR</abbr></td>
    <td>Crystal Ballroom</td>
</tr>
<tr>
<td>June 10th</td>
    <td>Seattle,<abbr title="Wshington">WA</abbr></td>
    <td>Crocodile Cafe</td>
</tr>
<tr>
<td>June 12th</td>
    <td>Sacramento,<abbr title="Califoria">CA</abbr></td>
    <td>Torch Club</td>
</tr>
<tr>
<td>June 17th</td>
    <td>Austin,<abbr title="Texas">TX</abbr></td>
    <td>Speakeasy</td>
</tr>
然后是js的
function onmouseChange(){
var trs=document.getElementsByTagName("tr");
for(var i=0;i<trs.length;i++){
trs[i].onmouseover=function(){
//addClass(trs[i],"omc");
addClass(this,"omc");
}
}
}
这个函数的意思就是当onmouseover的时候,为其加一个class="omc"的属性,通过设置omc的背景色来达到要求。
但是被我注掉的那段代码addClass(trs[i],"omc");为什么没有效果,而用this来指代就可以。谁能指教一下,谢谢

解决方案 »

  1.   

    我知道这个,但是不用this为什么不行
      

  2.   

    原因很简单 你的循环 只是为各自的tr注册了一个事件 而在事件触发的时候 js去调用function方法 此时哪有什么循环 连i变量都没有了 如果找到tr
      

  3.   

    trs[i].onmouseover=function(){
    //addClass(trs[i],"omc");
    addClass(this,"omc");
    }
    作用域范围的问题,trs[i] 在这函数里面获取不到东西
      

  4.   

    trs[i].onmouseover=function(){
      return addClass(trs[i],"omc"); 
    }()
    这样可以
      

  5.   


    i变量时一直存在的,只是变成最大值了
    你说的也对 反正就是写法不多啦
    i是最大值?但是我试过;最后一行并没有变色;但是我把代码改成这样
    function onmouseChange(){
    var trs=document.getElementsByTagName("tr");

    for(var i=0;i<trs.length;i++){
    var cure=trs[i];
    cure.oldClassName=trs[i].className;
    cure.onmouseover=function(){
    addClass(cure,"omc");
    //addClass(this,"omc");
    }
    cure.onmouseout=function(){
    this.className=this.oldClassName;
    }
    }
    }
    这样改;就是只有最后一行变色了;为什么那个i会变成最大值呢