代码如下:
<!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>无标题文档</title><style type="text/css">
<!--
#Layer1 {
position:absolute;
left:3px;
top:7px;
width:451px;
height:35px;
z-index:1;
}
-->
</style>
</head>
<SCRIPT LANGUAGE="JavaScript">
<!--
function changeColor(j){
var arr = document.all("state");
alert(arr.length);
for(var i=0;i <arr.length;i++){     if(i==j-1){ 
   arr[i].style.cssText="font-weight:bold;color:#ffcc00;";
   arr[i].style.backgroundColor="#006600";       
    } else{ 
      arr[i].style.cssText="font-weight:bold;color:#ffffff;";
arr[i].style.backgroundColor="#33cc00"; 
    } 


//-->
</SCRIPT><body>
  <table width="437" border="0" cellspacing="0">
    <tr>
      <TD height="22" align="center" bgcolor="#33cc00" id=state style="font-weight:bold;color:#ffffff;" onMouseMove="changeColor(1)">首页</TD>
      <TD align="center" bgcolor="#33cc00" style="font-weight:bold;color:#ffffff" onMouseMove="changeColor(2)">旅游资讯</td>
      <TD align="center" bgcolor="#33cc00" style="font-weight:bold;color:#ffffff" onMouseMove="changeColor(3)">旅游景点</td>
      <TD align="center" bgcolor="#33cc00" style="font-weight:bold;color:#ffffff" onMouseMove="changeColor(4)">酒店查询</td>
      <TD align="center" bgcolor="#33cc00" style="font-weight:bold;color:#ffffff" onMouseMove="changeColor(5)">联系我们</td>
    </tr>
  </table>
</body>
</html>运行出来是undefined

解决方案 »

  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>无标题文档 </title> <style type="text/css"> 
    <!-- 
    #Layer1 { 
    position:absolute; 
    left:3px; 
    top:7px; 
    width:451px; 
    height:35px; 
    z-index:1; 

    --> 
    </style> 
    </head> 
    <SCRIPT LANGUAGE="JavaScript"> 
    <!-- 
    function changeColor(j){ 
    var arr = document.all["state"]; 
    alert(arr.length); 
    for(var i=0;i <arr.length;i++){     if(i==j-1){ 
      arr[i].style.cssText="font-weight:bold;color:#ffcc00;"; 
      arr[i].style.backgroundColor="#006600";      
        } else{ 
          arr[i].style.cssText="font-weight:bold;color:#ffffff;"; 
    arr[i].style.backgroundColor="#33cc00"; 
        } 


    //--> 
    </SCRIPT> <body> 
      <table width="437" border="0" cellspacing="0"> 
        <tr> 
          <TD height="22" align="center" bgcolor="#33cc00" id="state" style="font-weight:bold;color:#ffffff;" onMouseMove="changeColor(1)">首页 </TD> 
          <TD id="state" align="center" bgcolor="#33cc00" style="font-weight:bold;color:#ffffff" onMouseMove="changeColor(2)">旅游资讯 </td> 
          <TD id="state" align="center" bgcolor="#33cc00" style="font-weight:bold;color:#ffffff" onMouseMove="changeColor(3)">旅游景点 </td> 
          <TD id="state" align="center" bgcolor="#33cc00" style="font-weight:bold;color:#ffffff" onMouseMove="changeColor(4)">酒店查询 </td> 
          <TD id="state" align="center" bgcolor="#33cc00" style="font-weight:bold;color:#ffffff" onMouseMove="changeColor(5)">联系我们 </td> 
        </tr> 
      </table> 
    </body> 
    </html>
      

  2.   

    你是在firefox下测试的吧?firefox不支持document.all。
    IE6,IE7,IE8,chrome,sarafi3.2,opera9都支持这个属性,但唯独firefox不支持。其他获取元素的方式有:
    getElementsByTagName("*") 可以得到得到所有元素的集合
    getElemntById 可以按id得到某一元素
    getElementsByName 可以得到按name属性得到某一元素如果你的页面对JS加载量没限制,推荐你试用一下jQuery框架,里面的选择器非常实用。
    http://jquery.com/