Use JS to display different colors in different rows in the table.

Source: Internet
Author: User

(Conversion) Use JS to display different colors in the table by line.

Use JS to display different colors in the table by line

First:
<Style>
Tr {bgColor: expression (
This. bgColor = (this. rowIndex) % 2 = 0 )? 'White ': 'yellow ');
}
</Style>
<Table id = "oTable" width = "100" border = "1" style = "border-collapse: collapse;">
<Tr> <td> & nbsp; </td> </tr>
<Tr> <td> & nbsp; </td> </tr>
<Tr> <td> & nbsp; </td> </tr>
<Tr> <td> & nbsp; </td> </tr>

Second:
<Script language = "javascript">
Var oTable = document. all ['otable'];
For (var I = 0; I <oTable. rows. length; I ++ ){
OTable. rows [I]. bgColor = (I % 2 = 0 )? 'White ': 'yellow ';
}
</Script>
<Table id = "oTable" width = "100" border = "1" style = "border-collapse: collapse;">
<Tr> <td> & nbsp; </td> </tr>
<Tr> <td> & nbsp; </td> </tr>
<Tr> <td> & nbsp; </td> </tr>

The third (the second release ):
Replace it with the bucket variable: Memory change time
Var isToggled = false;

Var oTable = document. all ['otable'];
For (var I = 0; I <oTable. rows. length; I ++ ){
IsToggled =! IsToggled;
OTable. rows [I]. bgColor = isToggled? "White": "yellow ";
}


Change the background color when the mouse passes
<Table> <tr> <td width = "100" height = "100" onmouseover = "style. backgroundColor = '# c86f70' "onmouseout =" style. backgroundColor = '# DDC676' "bgcolor =" # DDC676 "> ihandu </td> </tr> </table>


My method 1:
<! -- <Script language = "javascript">
Window. onload = function () {// different colors are displayed in the row separation table.
Var tab = document. getElementById ('tab ');
For (var I = 0; I <tab. rows. length; I ++ ){
Tab. rows [I]. bgColor = (I % 2 = 0 )? 'White': '# e6e6e6 ';
}
}
</Script>
-->


My method 2:
<Tr height = "22px" align = center style = "c: if test = '$ {status. count % 2 = 0} '>#ffffff </c: if> <c: if test =' $ {status. count % 2! = 0} '> # E6E6E6 </c: if> ">

 

Appendix:

<Script src = "jquery-1.7.min.js"> </script>
<Script>
$ (Document). ready (function (){
SetTableRowColor ();

});
// Use CSS to control the color of the even row
Function SetTableRowColor ()
{

$ ("# Table tr: odd" ).css ("background-color", "# e6e6fa ");
$ ("# Table tr: even" ).css ("background-color", "# fff0fa ");
}
</Scirpt>

 

Related Article

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.