【发布时间】:2016-06-02 13:51:44
【问题描述】:
我正在尝试找到一种方法来更改一个类的 css,前提是它位于具有 4 个 td 的表内。 该代码可能会更好地解释我的问题。
HTML
<table class="myclass">
<tr>
<td>1 <span class="myclass2">xxx</span></td>
<td>2 <span class="myclass2">xxx</span></td>
<td>3 <span class="myclass2">xxx</span></td>
</tr>
</table>
<table class="myclass">
<tr>
<td>1 <span class="myclass2">xxx</span></td>
<td>2 <span class="myclass2">xxx</span></td>
<td>3 <span class="myclass2">xxx</span></td>
<td>4 <span class="myclass2">xxx</span></td>
</tr>
</table>
CSS
.myclass td:first-child:nth-last-child(3),td:first-child:nth-last-child(3) ~ td {
background-color:white !important;
}
.myclass td:first-child:nth-last-child(4),td:first-child:nth-last-child(4) ~ td {
background-color:red !important;
}
.myclass2 {
color:blue;
}
JSFiddle 已准备就绪:JSFIDDLE HERE
我想要做的是仅更改具有 4 个 TD 的表中包含的元素的“myclass2”样式,而不是具有 3 个 TD 的表。 这可能吗?
【问题讨论】:
-
我不相信这是可能的,因为确定
<table>中有四个单元格的唯一方法是使用第四个或最后一个<td>;并且(到目前为止)还没有办法选择第四个<td>的前一个兄弟姐妹。据我所知,这是一个需要以某种方式使用 JavaScript 的问题。 -
@Vohuman 这是错误的。见这里:stackoverflow.com/questions/8720931/…。我的例子也证明这是可能的。
-
@DavidThomas 谢谢。我正在开发一个 HTML 电子邮件,所以我将无法使用 js :-)
-
嗨 MeV,所以如果表有 4 个 TD,它应该有 myclass2 类,而其他不应该有这个类?
-
如果您正在开发 HTML 电子邮件,请注意许多电子邮件客户端会删除或忽略
标签: html css html-email