The steps are as follows:
1. Use GetID to get the ID of the table
2. Get the number of rows to the table
3, using a loop to traverse the number of rows, using the IF condition to determine the parity, with the. style.backgroundcolor= Color Setting color
Complete
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <MetaCharSet= "UTF-8">5 <title>Table interlaced color changing function</title>6 <Scripttype= "Text/javascript">7 window.onload= function(){8 //1. Get the form9 varTab=document.getElementById ("Tal");Ten //2. Get the number of tbody in the table One varLen=tab.tbodies[0].rows.length A //alert (len) - //3. Start the loop, set the color - for(varI= 0; I<Len;i++){ the if(i%2==0){ - tab.tbodies[0].rows[i].style.backgroundcolor= "Red"; - }Else{ - tab.tbodies[0].rows[i].style.backgroundcolor= "Green"; + } - } + } A </Script> at </Head> - <Body> - <TableBorder= "1px"width= "500px"Height= "50px"Align= "Center"ID= "Tal"> - <thead> - <TR> - <th>Number</th> in <th>Name</th> - <th>Age</th> to </TR> + </thead> - <tbody> the <TR> * <TD>1</TD> $ <TD>Tom</TD>Panax Notoginseng <TD>17</TD> - </TR> the <TR> + <TD>2</TD> A <TD>John doe</TD> the <TD>18</TD> + </TR> - <TR> $ <TD>3</TD> $ <TD>Harry</TD> - <TD>19</TD> - </TR> the <TR> - <TD>4</TD>Wuyi <TD>Zhao Liu</TD> the <TD>20</TD> - </TR> Wu <TR> - <TD>5</TD> About <TD>Tianqi</TD> $ <TD>21st</TD> - </TR> - <TR> - <TD>6</TD> A <TD>Zhongba</TD> + <TD>28</TD> the </TR> - </tbody> $ </Table> the </Body> the </HTML>
Native JS implementation of table interlaced color change