<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>cc pj1 </title> 
<script language="javascript"> 
var x; 
 _cc=function(){ 
var tds=document.getElementById("t").childNodes[0].childNodes[0]; 
for(var i=0;i <tds.childNodes.length;i++) 

tds.childNodes[i].onmouseover=function() 

this.style.backgroundColor="#FFF9C3"; 
//alert(i); //这里

tds.childNodes[i].onmouseout=function() 

this.style.backgroundColor=""; 



</script> 
</head> 
<body> 
<table width="500" border="1" cellspacing="0" cellpadding="0"  id="t" onmouseover="_cc()"> 
<tbody> 
   <tr align="center"> 
     <td>bbb1 </td> 
     <td>bbb2 </td> 
     <td>bbb3 </td> 
   </tr> 
   </tbody> 
</table> 
</body> 
</html> 

解决方案 »

  1.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
    <title>cc pj1 </title> 
    <script language="javascript"> 
    var x; 
     _cc=function(){ 
    var tds=document.getElementById("t").childNodes[0].childNodes[0]; 
    for(var i=0;i <tds.childNodes.length;i++) 

    tds.childNodes[i].onmouseover=new Function("this.style.backgroundColor=\"#FFF9C3\"; alert(" + i + ")");
    tds.childNodes[i].onmouseout=function() 

    this.style.backgroundColor=""; 



    </script> 
    </head> 
    <body> 
    <table width="500" border="1" cellspacing="0" cellpadding="0"  id="t" onmouseover="_cc()"> 
    <tbody> 
       <tr align="center"> 
         <td>bbb1 </td> 
         <td>bbb2 </td> 
         <td>bbb3 </td> 
       </tr> 
       </tbody> 
    </table> 
    </body> 
    </html> 
      

  2.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
    <title>cc pj1 </title> 
    <script language="javascript"> 
    var x; 
     _cc=function(){ 
    var tds=document.getElementById("t").childNodes[0].childNodes[0]; 
    for(var i=0;i <tds.childNodes.length;i++) 

    tds.childNodes[i].onmouseover=showrow(tds.childNodes[i],i)tds.childNodes[i].onmouseout=function() 

    this.style.backgroundColor=""; 



    function showrow(o,n){ 
      o.style.backgroundColor="#FFF9C3"; 
      alert(n); 
    }
    </script> 
    </head> 
    <body> 
    <table width="500" border="1" cellspacing="0" cellpadding="0"  id="t" onmouseover="_cc()"> 
    <tbody> 
       <tr align="center"> 
         <td>bbb1 </td> 
         <td>bbb2 </td> 
         <td>bbb3 </td> 
       </tr> 
       </tbody> 
    </table> 
    </body> 
    </html> 
      

  3.   

    这个问题 是由于 i在最后alert是 引用的 最后一个值
    虽然 前面初始化的时候 是alert(i)
    但这个是一个表达式 而不是 alert(1)  alert(2)
    到最后执行时候 是alert(i) 这个时候 i是3 所以就出现了 开始情况要解决的办法 是 在定义的时候 把 i 转换为一个字符值 到最后 进行alert