<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script language="javascript" type="text/javascript">
        var oldRow = null;
        window.onload=function()
        {
            var tblId = "tbl";
            var tbl = $(tblId);
            if(tbl!=null)
            {
                for(var i=1;i<tbl.rows.length;i++)
                {
                    tbl.rows[i].onclick=function(){clickRow(this)};
                }
            }
        }
        
        function clickRow(row)
        {
            var colorHighlt = "red";
            if(oldRow!=row)
            {
                if(oldRow!=null)
                {
                    oldRow.style.backgroundColor = "";
                }
                row.style.backgroundColor = colorHighlt;
            }else
            {
                row.style.backgroundColor=(row.style.backgroundColor==""?"red":"");
            }
            
            oldRow = row;
        }
        
        function $(id)
        {
            return document.getElementById(id);
        }
    </script>
</head>
<body><table border='1px' width='500px' id="tbl"> 
<tr>
    <th>列1</th>
    <th>列2</th>
</tr>
<tr> 
<td class='a'>1111 </td> 
<td class='a'>2222 </td> 
</tr> 
<tr> 
<td class='a'>3333 </td> 
<td class='a'>44444 </td> 
</tr> 
<tr> 
<td class='a'>5555 </td> 
<td class='a'>66666 </td> 
</tr> </table> </body>
</html>

解决方案 »

  1.   


    <html>
    <head>
    <script language = "javascript">

    function changeCss()
    {
    var tab = document.getElementById("tab1");
    if(tab){
    for(var i = 1;i<tab.rows.length;i++){
    if(i % 2 === 0)
    continut;
    else
    tab.rows[i].style.background = "red";
    }
    }
    }
    window.onload = changeCss();
    </script> </head>
    <body>
    <table border='1px' id = "tab1" width='500px'> <tr> 
    <td class='a'>1111 </td> 
    <td class='a'>2222 </td> 
    </tr> 
    <tr> 
    <td class='a'>3333 </td> 
    <td class='a'>44444 </td> 
    </tr> 
    <tr> 
    <td class='a'>5555 </td> 
    <td class='a'>66666 </td> 
    </tr> 
    <tr> 
    <td class='a'>55551 </td> 
    <td class='a'>666662 </td> 
    </tr> 
    <tr> 
    <td class='a'>55552 </td> 
    <td class='a'>666668</td> 
    </tr> </table> 
    <body>
    <html>
      

  2.   

    sorry,我以为是单击变色,呵呵<style  type="text/css">
    .a{background-color:red;}
    </style>
    <script language = "javascript">
        
    window.onload = function()
    {
        var tbl=document.getElementById("tbl");
        for(var i=1;i<tbl.rows.length;i++)
        {
            if(i%2!=0)
            {
                tbl.rows[i].className="a";
            }
        }
    }
    </script>    
    <table border='1px' width='500px' id="tbl"> 
    <tr>
        <th>列1</th>
        <th>列2</th>
    </tr>
    <tr> 
    <td>1111 </td> 
    <td>2222 </td> 
    </tr> 
    <tr> 
    <td>3333 </td> 
    <td>44444 </td> 
    </tr> 
    <tr> 
    <td>5555 </td> 
    <td >66666 </td> 
    </tr> 
    <tr> 
    <td>5555 </td> 
    <td >66666 </td> 
    </tr> 
    <tr> 
    <td>5555 </td> 
    <td >66666 </td> 
    </tr> 
    </table> 
      

  3.   


    强.思路就是自动对行编号,然后引入css的class
      

  4.   

    xiongzhijian,你的代码有两个错误1,是continue,而不是continut
    2, window.onload的时候是调用函数名,而不是函数调用。感谢xiongzhijian和CutBut的回答!