js代码;简单的隔行变色;不知道问题在哪里;求请教

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script>
window.onload=function(){

   var qwe=document.getElementById('tab')    
   for(var i=0; i<qwe.tBodies[0].rows.length;i++)

   {         if(i%2)
       {
           qwe.tbodies[0].rows[i].style.background=''
           }        else{
           qwe.tbodies[0].rows[i].style.background='#ccc'
            }
       }
   }

</script>
</head>

<body>

序号名字年龄
1张三23
2赵六34
3王五12
4李四23

</body>
</html>


肥皂起泡泡
浏览 578回答 1
1回答

繁花如伊

<html><head><meta charset="utf-8"><title>无标题文档</title><script>window.onload=function(){&nbsp; &nbsp;var qwe=document.getElementById('tab');&nbsp; &nbsp;var tbRows = qwe.tBodies[0].rows;&nbsp; &nbsp;for(var i=0; i<tbRows.length;i++)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;if(i%2)&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tbRows[i].style.background='#fff';&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tbRows[i].style.background='#ccc';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}</script></head><body><table id="tab" width="500px"><thead><tr><td>序号</td>&nbsp; &nbsp; &nbsp; <td>名字</td>&nbsp; &nbsp; &nbsp; <td>年龄</td>&nbsp; &nbsp;</tr></thead><tbody><tr><td>1</td>&nbsp; &nbsp; &nbsp; &nbsp;<td>张三</td>&nbsp; &nbsp; &nbsp; &nbsp;<td>23</td>&nbsp; &nbsp; &nbsp; </tr><tr><td>2</td>&nbsp; &nbsp; &nbsp; &nbsp;<td>赵六</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>34</td>&nbsp; &nbsp; &nbsp; </tr><tr><td>3</td>&nbsp; &nbsp; &nbsp; &nbsp;<td>王五</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>12</td>&nbsp; &nbsp; &nbsp;</tr><tr><td>4</td>&nbsp; &nbsp; &nbsp; &nbsp;<td>李四</td>&nbsp; &nbsp; &nbsp; &nbsp;<td>23</td>&nbsp; &nbsp; &nbsp; </tr></tbody></table></body></html>把qwe.tBodies[0].rows定义在for循环外
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript