使用dom实现隔行变色

来源:互联网 发布:topsview监控软件 编辑:程序博客网 时间:2024/05/06 13:58

<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<head>

<metahttp-equiv="Content-Type" content="text/html;charset=utf-8" />

<title>隔行变色</title>

<script language="javascript"type="text/javascript">

  function getColor(){

            varta=document.getElementsByName("tab1");

            vartr1=document.getElementsByTagName("tr");

              //alert(color);

                    //alert(tr1.length);

              for(var i=0;i<tr1.length;i++){

                             

                              if(i%2==0){

                                       //偶数为红色

                                       tr1[i].style.backgroundColor="red";

                                      

                                       }else{

                                                 //奇数为绿色

                                                tr1[i].style.backgroundColor="green";

                                                

                                                 }

                             

                             }

           

            }

 

 

</script>

</head>

 

<body>

<table width="300" border="1"cellpadding="0" id="tab1">

 <tr class="A">

   <td colspan="4">&nbsp;</td>

   

 </tr>

 <tr class="B">

 <td colspan="4">&nbsp;</td>

 </tr>

 <tr class="C">

  <td colspan="4">&nbsp;</td>

 </tr>

 <tr class="D">

  <td colspan="4">&nbsp;</td>

  </tr>

</table>

<input type="button"value="点就变色"onclick="getColor()">

</body>

</html>


原创粉丝点击