风格TD喜欢TR奇/偶 [英] Style TD like TR odd/even

查看:117
本文介绍了风格TD喜欢TR奇/偶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据奇数/偶数更改td的文字颜色,例如 http://jsfiddle.net/N9gEG/

I need to change td's text color based on odd/even, like this http://jsfiddle.net/N9gEG/

其实我有一个类来做这个,但是我想从css做

Actually I have a class which do this, but, I want to do from css

<table>
    <tr>
        <td>RED</td>
        <td class="foo">BLUE</td>
        <td>RED</td>
        <td class="foo">BLUE</td>
    </tr>
</table>

tr 请遵循代码: table tr:nth-​​child(even)

推荐答案

td {
  color: blue;
}
td:nth-child(even) {
  color: red;
}

这是因为规则特异性。更具体的CSS规则获胜。 td 没有任何其他比 td:nth-​​child(even)更具体,因此它适用于奇< td>

This works because of rule specificity. The more specific CSS rule wins. td without anything else is less specific than td:nth-child(even), so it applies to the odd <td>s automatically.

这篇关于风格TD喜欢TR奇/偶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆