JS realizes the interlaced color-------Day40

Source: Internet
Author: User

So started their own second project, the performance appraisal system, a project team, the first to start, but also this painful interface design, can not change the existing template, only on the existing basis to play, my poor aesthetic in this small space suddenly so helpless, okay, There are always some places still need to add some effect, like this interlaced color, not very practical.

Table in this div+css raging era some decline, but encountered multiple columns of data display, it still has incomparable convenience, we first write down a

<table id= "TB" width= "900px;" border= "1" >    <tbody>    <tr>    <td>213</td>    <td>234</td>    </tr>    <tr>    <td>213</td>    <td>234 </td>    </tr>    <tr>    <td>213</td>    <td>234</td>    </tr>    <tr>    <td>213</td>    <td>234</td>    </tr>    <tr>    <td>213</td>    <td>234</td>    </tr>    </tbody>    </table>
so let's take a look at the effect and add border here to make it easier to see


And then we're going to make the interlaced color by JavaScript.

<script type= "Text/javascript" >window.onload=function () {var Tb=document.getelementbyid ("TB");// Gets the Table object var tbody=tb.getelementsbytagname ("Tbody") [0];//gets the tbody part of the table Var trs=tbody.getelementsbytagname ("tr ");//gets all the rows for the Tbody section for (Var i=0;i<trs.length;i++) {if (i%2==0) {//judgment can be divisible by 2, i.e. the color of even lines is gray trs[i].style.background=" # CCC ";//The corresponding line, the object's style setting}}};</script>
Let's take a look at the effect here.


In this way, a beautiful interlaced color is realized, this seemingly interview problem often appears, after all, is a relatively basic thing, but I now learn, ah, sin sin ....


Every moment is new, come on


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.