日期:2014-05-16  浏览次数:20409 次

高分求解!jquery为表格自动添加class
表格一开始时没有任何class的,想通过jquery添加.有以下要求:
1.为所有加上class,实现迭代计数,如果到下一个节点,重新计数。(tr为row1 th和td为col1)
2.tbody偶数行加上alt
3.若为第一或最后一项,则加上first/last
  若只有一项,则first last
4.注意一个页面有可能有多个table

生成的样例如下:
HTML code
<!DOCTYPE html>
<head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        //
    </script>
</head>


<body>
<table>
        <thead>
            <tr>
                <th class="col0 first">A</th>
                <th class="col1">B</th>
                <th class="col2">C</th>
                <th class="col3">D</th>
                <th class="col4 last">E</th>
            </tr>
        </thead>
        <tbody>
            <tr class="row0 first">
                <td class="col0 first">A</td>
                <td class="col1">B</td>
                <td class="col2">C</td>
                <td class="col3">D</td>
                <td class="col4 last">E</td>
            </tr>
            <tr class="row1 alt">
                <td class="col0 first">A</td>
                <td class="col1">B</td>
                <td class="col2">C</td>
                <td class="col3">D</td>
                <td class="col4 last">E</td>
            </tr>
            <tr class="row2">
                <td class="col0 first">A</td>
                <td class="col1">B</td>
                <td class="col2">C</td>
                <td class="col3">D</td>
                <td class="col4 last">E</td>
            </tr>
            <tr class="row3 last alt">
                <td class="col0 first">A</td>
                <td class="col1">B</td>
                <td class="col2">C</td>
                <td class="col3">D</td>
                <td class="col4 last">E</td>
            </tr>
        </tbody>
        <tfoot>
            <tr class="row0 first">
                <td class="col0 first">A</td>
                <td class="col1">B</td>
                <td class="col2">C</td>
                <td class="col3">D</td>
                <td class="col4 last">E</td>
            </tr>
        </tfoot>
    </table>
    
    <table>
        <thead>
            <tr>
                <th class="col0 first">A</th>
                <th class="col1">B</th>
                <th class="col2">C</th>
                <th class="col3">D</th>
                <th class="col4 last">E</th>
            </tr>
        </thead>
        <tbody>
            <tr class="row0 first">
                <td class="col0 first last">A</td>
                <td class="col1">B</td>
                <td class="col2">C</td>
                <td class="col3">D</td>
                <td class="col4 last">E</td>