jQuery Selector :nth-child() highlight alternate table row

Description

jQuery Selector :nth-child() highlight alternate table row

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Zebra Striped Table with jQuery</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
    $(document).ready(function(){
        // Apply alt class on alternate table row
        $("table tbody tr:nth-child(2n)").addClass("alt");
    });/*from  w  w  w.ja  va2  s  . com*/
</script>
<style>
    table{
        margin: 30px;
        border-collapse: collapse;
    }
    table tr{
        border-bottom: 1px solid #666;
    }
    table th, table td{
        padding: 10px;
    }
    /* Highlighter class */
    .alt{
        background: #cdcdcd;
    }
</style>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>Row</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>1</td>
                <td>John</td>
                <td>Carter</td>
                <td>d@mail.com</td>
            </tr>
            <tr>
                <td>2</td>
                <td>Peter</td>
                <td>Parker</td>
                <td>peterparker@mail.com</td>
            </tr>
            <tr>
                <td>3</td>
                <td>John</td>
                <td>Rambo</td>
                <td>a@mail.com</td>
            </tr>
            <tr>
                <td>4</td>
                <td>Harry</td>
                <td>Potter</td>
                <td>b@mail.com</td>
            </tr>
            <tr>
                <td>5</td>
                <td>Percy</td>
                <td>Jackson</td>
                <td>c@mail.com</td>
            </tr>
        </tbody>
    </table>
</body>
</html>



PreviousNext

Related