How to create a table Zebra Stripes effect with jQuery
Written on November 20, 2008 at 2:59 pm by
mkyong
Here is a simple example to show how to create a table Zebra Stripes effect with jQuery.
<html> <head> <title>jQuery Zebra Stripes</title> </head> <script type="text/javascript" src="jquery-1.2.6.min.js"></script> <script type="text/javascript"> $(function() { $("table tr:nth-child(even)").addClass("striped"); }); </script> <style type="text/css"> body,td { font-size: 10pt; } table { background-color: black; border: 1px black solid; border-collapse: collapse; } th { border: 1px outset silver; background-color: maroon; color: white; } tr { background-color: white; margin: 1px; } tr.striped { background-color: coral; } td { padding: 1px 8px; } </style> <body> <table> <tr> <th>ID</th> <th>Fruit</th> <th>Price</th> </tr> <tr> <td>1</td> <td>Apple</td> <td>0.60</td> </tr> <tr> <td>2</td> <td>Orange</td> <td>0.50</td> </tr> <tr> <td>3</td> <td>Banana</td> <td>0.10</td> </tr> <tr> <td>4</td> <td>strawberry</td> <td>0.05</td> </tr> <tr> <td>5</td> <td>carrot</td> <td>0.10</td> </tr> </table> </body> </html>
In jQuery, the table zebra stripes effect is achieved with one statement.
$(function() {
$("table tr:nth-child(even)").addClass("striped");
});P.S nth-child(even)”).addClass(“striped”) = every even row add “striped” CSS class dynamically.
This article was posted in JQuery category.
Oracle Magazine - Free Magazine
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for Java's developers and DBAs, and more.
Securing & Optimizing Linux: The Hacking Solution - Free Guide
A comprehensive collection of Linux security products and explanations in the most simple and structured manner on how to safely and easily configure and run many popular Linux-based applications and services.
The Windows 7 Guide: From Newbies to Pros - Free Guide
In this 46 page guide you will be introduced to Windows 7 and what it has to offer. This guide will go over the software compatible issues, you will learn about the new taskbar, how to use and customize Windows Aero, what Windows 7 Libraries are all about, what software is included in Windows 7, and how easy networking is with Windows 7 along with other topics.
All Java Tutorials
- Java Core Technology - Java RegEx, Java XML, Java I/O, Java Misc
- J2EE Frameworks - Hibernate, Spring 2.5, Spring MVC, Struts 1.x, Struts 2.x
- Build Tools - Maven, Archiva
- Unit Test - jUnit, TestNG
- Client Scripts - jQuery
nice.. it’s very helpful for newbie like me..
[...] P.S The ‘even’ and ‘odd’ are always used to create table zebra stripes effect. [...]