html - 如果元素包含特定类型的子元素,如何使用 CSS 选择器消除元素

标签 html css

让我举个例子来澄清我的问题。

假设我有如下表格:

<table>
  <tr>
    <th>header1</th>
    <th>header2</th>
  </tr>

  <tr>
    <td>elem1</td>
    <td>elem2</td>
  </tr>

  <tr>
    <td>elem3</td>
    <td>elem4</td>
  </tr>
</table>

我想显示包含 <td> 的表格行元素是可点击的,所以我试图将鼠标指针光标悬停在 <tr> 上时更改为手形包含 <td>元素。

我可以通过如下设置样式来做到这一点:

table tr:not(:first-child) {cursor:pointer;}

表的第一个 child 通常是 <tr>包含 <th> .

问题来了,css样式怎么写消除<tr>包含 <th>以更直观的方式作为直接 child ?

最佳答案

利用 theadtbody 元素,然后使用 CSS 仅定位 tbody 中的行:

table tbody tr {
  cursor: pointer;
}
<table>
  <thead>
    <tr>
      <th>header1</th>
      <th>header2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>elem1</td>
      <td>elem2</td>
    </tr>
    <tr>
      <td>elem3</td>
      <td>elem4</td>
    </tr>
  </tbody>
</table>

关于html - 如果元素包含特定类型的子元素,如何使用 CSS 选择器消除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49975037/

相关文章:

css - 需要为像骑士骑士灯泡这样的图像制作动画

css - 不同的 Material Icons 样式不对齐

jquery - 使用类将 vclick 绑定(bind)到 url

html - 使文本始终位于框的右上角

html - css 边距未响应

javascript - ckEditor 不在 IE 中保存链接

html - 宽度 100% 超过屏幕的 100%

html - 背景 :cover covered by div

javascript - 选择标签、替换值 (JavaScript)

html - 子 Div 的显示 :none is not working