java Html&JavaScript面试题:用table显示n条记录,每3行换一次颜色,即1,2,3用红色字体,4,5,6用绿色字体,7,8,9用红颜色字体。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>JavaScript
<body>
    <table id="tb">
        <tr>
            <td>111111111111111</td>
        </tr>
        <tr>
            <td>222222222222</td>
        </tr>
        <tr>
            <td>33333333333</td>
        </tr>
        <tr>
            <td>44444444444</td>
        </tr>
        <tr>
            <td>55555555555</td>
        </tr>
        <tr>
            <td>66666666</td>
        </tr>
        <tr>
            <td>7777777777</td>
        </tr>
        <tr>
            <td>8888888</td>
        </tr>
        <tr>
            <td>9999999999</td>
        </tr>
        <tr>
            <td>101010101010</td>
        </tr>
    </table>
</body>
<script type="text/javascript">
    window.onload=function()
          {
                  var tb =document.getElementById("tb");
                  var rows =tb.getElementsByTagName("tr");
                  for(i=0;i<rows.length;i++)
                  {
                          var j = parseInt(i/3);//用了自带函数parseInt解析成整数
                          alert(i/3);//不解析的值
                          if(j%2==0){
                              rows[i].style.backgroundColor="#f00";//背景红色
                              rows[i].style.color="#fff";          //字体白色
                          }else{
                              rows[i].style.backgroundColor="#0f0";//绿色
                              rows[i].style.color="#00f";          //蓝色
                          } 
                  }
          }
</script>
</html>

 

posted @ 2018-01-30 18:42  Advancing-Swift  阅读(323)  评论(0编辑  收藏  举报