用table显示n条记录,每3行换一次颜色,即1,2,3用红色字体,4,5,6用绿色字体,7,8,9用红颜色字体。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  
<!DOCTYPE HTML>  
<html>  
  <head>  
      <script type="text/javascript">  
        window.onload=function() {  
               var tbl = document.getElementById("tbl");  
               var rows = tbl.getElementsByTagName("tr");  
               for(i=0;i<rows.length;i++) {  
                      var j = parseInt(i/3); // 此语句的效果是,以3个为单位,000 111 222 333 444 ...  
                      if(j%2==0) rows[i].style.backgroundColor="#f00";  
                      else  rows[i].style.backgroundColor="#0f0";  
               }  
        };  
      </script>  
  </head>  
  <body>  
    <table id="tbl" border="1">  
       <tr><td>1</td></tr>  
       <tr><td>2</td></tr>  
       <tr><td>3</td></tr>  
       <tr><td>4</td></tr>  
       <tr><td>5</td></tr>  
       <tr><td>6</td></tr>  
       <tr><td>7</td></tr>  
       <tr><td>8</td></tr>  
       <tr><td>9</td></tr>  
       <tr><td>10</td></tr>  
       <tr><td>11</td></tr>  
       <tr><td>12</td></tr>  
       <tr><td>13</td></tr>  
       <tr><td>14</td></tr>  
       <tr><td>15</td></tr>  
       <tr><td>16</td></tr>  
       <tr><td>17</td></tr>  
       <tr><td>18</td></tr>  
    </table>  
  </body>  
</html>  

 

posted on 2017-10-27 17:26    阅读(1093)  评论(0编辑  收藏  举报