简体   繁体   English

如何遍历表并为trs的tds定义类?

[英]How to iterate over table and define class for tds of trs?

I have a table like that: 我有一张这样的桌子:

<table id="myTable" cellspacing="0" cellpadding="10" border="0" width="100%">
<tbody>
....
<tr style="color: blue;" id="bankRecord377">      
  <td align="center" class="styleOdd"> <input type="checkbox" value="377" name="377"></td>
  <td align="center" class="styleOdd">377</td>
  <td align="center" class="styleOdd"></td>
  <td align="center" class="styleOdd">391</td>
</tr>
....
<tr style="color: blue;" id="bankRecord386">     
  <td align="center" class="styleEven"> <input type="checkbox" value="386" name="386"></td>
  <td align="center" class="styleEven">386</td>
  <td align="center" class="styleEven"></td>
  <td align="center" class="styleEven">396</td>
</tr>
...
<tr style="color: blue;" id="bankRecord322">     
  <td align="center" class="styleEven"> <input type="checkbox" value="322" name="386"></td>
  <td align="center" class="styleEven">322</td>
  <td align="center" class="styleEven"></td>
  <td align="center" class="styleEven">314</td>
</tr>
...
</tbody>
</table>

class="styleOdd" makes row gray background class="styleEven" makes row background blue. class="styleOdd"使行背景class="styleOdd"灰色class="styleEven"使行背景class="styleEven"蓝色。 I iterate over that table with Struts2 and define classes but user can remove some of table rows when he sees that HTML file. 我使用Struts2遍历了该表并定义了类,但是当用户看到该HTML文件时,可以删除一些表行。 If user remove one of the table row, ex : 如果用户删除表行之一,例如:

<tr style="color: blue;" id="bankRecord386">
...
</tr>

Colors of background was gray, blue, gray. 背景颜色为灰色,蓝色,灰色。 However it is gray, gray now(because user removed a tr which includes classEven tds). 但是现在是灰色的(因为用户删除了一个包含classEven tds的tr)。

All in all what I want is iterate over that table again and defining classes styleOdd, styleEven, styleOdd, styleEven... again. 总而言之,我想要再次遍历该表并再次定义类styleOdd,styleEven,styleOdd,styleEven...。

How can I do it with JavaScript or JQuery? 如何使用JavaScript或JQuery做到这一点?

PS: I want to it for my table(which has id=myTable)'s every tds of trs. PS:我想为我的表(其ID = myTable)的所有ts tds设置它。

EDIT: I want it except for the first tr(and it's tds). 编辑:我想要它,除了第一个tr(和tds)。

You can use :even and :odd , but then you would iterate over the table rows twice. 您可以使用:even:odd ,但是随后您将遍历表行两次。 That might lead to unacceptable performance if your table has many rows. 如果您的表有很多行,可能会导致性能无法接受。

I'd suggest using each() instead: 我建议改用each()

$("#myTable tr").each(function(index, row) {
    var $cells = $("td", row);
    if (index & 1) {
        // Odd.
        $cells.removeClass("styleEven").addClass("styleOdd");
    } else {
        // Even.
        $cells.removeClass("styleOdd").addClass("styleEven");
    }
});

You can use the :odd selector to target odd rows. 您可以使用:odd选择器定位到奇数行。 (there is also a :even selector) (还有一个:even选择器)

$('.td:odd').class('odd'); 

Trigger this when removing a row from the table to update classes. 从表中删除一行以更新类时触发此操作。

There are also both CSS Selector but not widely supported. 同时也有CSS选择器,但并未得到广泛支持。

$('#myTable tr:odd td').toggleClass('styleOdd', true).toggleClass('styleEven', false);
$('#myTable tr:even td').toggleClass('styleOdd', false).toggleClass('styleEven', true);

I believe you could also do it automatically using CSS. 我相信您也可以使用CSS自动执行此操作。 Although it requires a fairly modern browser. 尽管它需要一个相当现代的浏览器。

Updated to take into account the table ID 更新以考虑到表ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM