简体   繁体   English

确定表中偶数/奇数行的更好方法

[英]A better way to determine even/odd rows in table

Say you have a webpage with following sample code: 假设您有一个包含以下示例代码的网页:

<tr class="even">
    <td>something1</td>
    <td colspan="1">somthing1.1</td>
</tr>


<tr class="odd">
    <td>something2</td>
    <td><b>something2.1</b></td>
</tr>

<tr class="even">
    <td>something3</td>
    <td><b>something3.1</b></td>
</tr>

These are not in a loop so I have to explicitly say 'even' 'odd'. 这些不是循环的,所以我必须明确地说“偶”“奇”。 Later on if we decide to add a new row between something2 and something3 then we need to change 'even' 'odd' for rest of the rows as well. 稍后,如果我们决定在something2和something3之间添加新行,那么我们还需要为其余行更改“偶数”“奇数”。

Is there way in css to do this automatically in IE6? 有没有办法在CSS中在IE6中自动执行此操作?

Currently this is my css code for even 目前这是我的CSS代码,甚至

.headerTable tr.even {
    font-weight : bold;
    font-size : 9pt;
    font-family : Arial,Helvetica,sans-serif,Verdana,Geneva;
    background-color: #FFFFFF;
    height: 20px;
    color: #000000;
}

I already had jQuery 我已经有了jQuery

Give jQuery a try. 尝试一下jQuery。 Then you can simply do this: 然后,您可以简单地执行以下操作:

   $("#myTable tbody tr:visible:even",this).addClass("even"); 
   $("#myTable tbody tr:visible:odd",this).addClass("odd");

The ":visible" selector isn't really necessary, but when you filter a table by making some rows hidden, the method will still work correctly. “:visible”选择器并不是真正必需的,但是当您通过隐藏一些行来过滤表时,该方法仍然可以正常工作。

The way to do this would be to use the nth-child(even) and (odd) rules. 这样做的方法是使用nth-child(even)和(odd)规则。 Unfortunately, and this should come as no surprise, IE6 does not support this. 不幸的是,这不足为奇,IE6不支持此功能。 So you have a few options: 因此,您有几种选择:

A) Use Javascript, with the obvious drawback of it not working for people with JS disabled: A)使用Javascript,明显的缺点是它不适用于禁用JS的人:

var rows = document.getElementById('mytable').getElementsByTagName('tr');
for(var x = 0; x < rows.length; x++) {
    rows[x].className = (x % 2 == 0) ? 'even' : 'odd';
}

If you expect to need more dynamic client side behavior on your application, you could then check a library like jQuery out. 如果您希望在应用程序上需要更多动态的客户端行为,则可以将类似jQuery的库签出。 For just this it is unnecessary, but it makes working with Javascript across browsers a breeze. 为此,这是不必要的,但它使跨浏览器使用Javascript变得轻而易举。 You would do the above with jQuery like it is shown in this answer. 您可以使用jQuery来完成上述操作,如此答案所示。

Depending on your audience, Javascript may be perfectly acceptable. 根据您的受众,JavaScript可能是完全可以接受的。 If it's not, then maybe you can... 如果不是,那么也许您可以...

B) Simplify your CSS and keep doing it manually. B)简化CSS并继续手动进行。 You can only mark the odd rows with a class, and then make the row default style the even styling. 您只能使用类标记奇数行,然后使该行的默认样式为偶数样式。 This will save you some work when moving things around. 这将在您四处移动时为您节省一些工作。

C) Generate the rows programmatically. C)以编程方式生成行。 It is really archaic to be entering data like that into a fixed table if you're going to be updating it often enough that this is a problem. 如果要经常更新数据到固定表中,这确实是很古老的做法,那么这将是一个问题。 I'm obviously oblivious to your circumstance, but it should be trivial to do this in a loop somehow with a simple language like PHP. 我显然没有理会您的情况,但是以某种简单的语言(例如PHP)以循环方式执行此操作应该是微不足道的。

D) Stop using a really outdated browser. D)停止使用真正过时的浏览器。 :) (This is only half joking, as I'm sure its out of your control) :)(这只是开玩笑的一半,因为我敢肯定,这是您无法控制的)

The best approach would be the :nth-child() pseudo-class . 最好的方法是:nth-child()伪类

But unfortunately it's not widely supported yet. 但不幸的是,它尚未得到广泛支持。 So you'll probably need to use JavaScript to do that. 因此,您可能需要使用JavaScript来做到这一点。

使用jQuery的偶数/奇数子实现

Yes. 是。 You can use JavaScript to run through these elements and reset their classes according to even/odd. 您可以使用JavaScript遍历这些元素,并根据偶数/奇数重置它们的类。

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

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