Native JS implementation of table interlaced color change

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.