Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html css:仅当表中有_Html_Css_Css Selectors - Fatal编程技术网

Html css:仅当表中有

Html css:仅当表中有,html,css,css-selectors,Html,Css,Css Selectors,有一个有效的css规则(jquery只是最后一次机会),它允许定义一个表中所有td的样式,只要有第th行 更具体地说 <table> <tr> <td> STYLE 1 HERE, my background color is white </td> </tr> </table> <table> <tr> <th> some t

有一个有效的css规则(jquery只是最后一次机会),它允许定义一个表中所有td的样式,只要有第th行

更具体地说

<table>
  <tr>
    <td>
      STYLE 1 HERE, my background color is white
    </td>
  </tr>
</table>

<table>
  <tr>
    <th>
      some text
    </th>
  </tr>
  <tr>
    <td>
      STYLE 2 HERE, my table has TH, my background color is red
    </td>
  </tr>
</table>
有办法吗

提前感谢,,
cicciopas在css中,您不能真正做到这一点。有
+
“相邻”选择,但这要求节点共享一个直接的公共父节点。如果你有一行混合的
,那么他们就有资格这样做。但是您的th和td单元位于不同的行中,因此它们不共享一个共同的父级

因此:

将为

<tr>
   <th>Look ma, I'm red!</th>
   <td>ooer, a header</td>
</tr>

看,妈妈,我是红色的!
哦,头球
因为它们共享相同的父级
,但是

<tr>
   <th>Header</th>
</tr>
<tr>
   <td>Dang, not red</td>
</tr>

标题
该死,不是红色的

不会,因为th/td节点有两个不同的父节点。

我认为您的代码有点错误,第一个背景白色实例的css应该是

tr,td {
     background-color:'ffffff;
}
如果我知道你想做的是在一个表中设置
标签的背景,而这个表中有一个
,那么通过纯css,你不能按照你想象的方式来做

然而,更好的方法是将一个类附加到您想要设置样式的
td
标记上,正如这个jsfiddle所演示的那样


恐怕您不能,因为我们需要“家长”选择器,例如:

tr < th ~ tr td { background-color: red; }
jQuery选择器中的
has()
正是我们希望的
css选择器所做的

tr,td {
     background-color:'ffffff;
}
tr < th ~ tr td { background-color: red; }
$("table:has(tr th)").find("tr td").css("background-color","red");