html - 在 table.th.tr 和 table.tbody.tr 中为两种类型的表选择 th

标签 html css html-table

我有数百个 HTML 文件,我需要在全局 css 中重新设置表格标题的样式。有些有头颅,有些没有。所以我可以设置 thead 的样式,但是对于没有 thead 的表,我设置第一个 tr 的样式。好吧,当然,也将样式应用于具有 thead 的表的第一行,从而导致这些表具有两行标题样式。 我已经尝试为那些没有 thead 的人设置正确的层次结构

tbody:first-child tr:first-child td 

但是我不能分别设计这两种类型的表格。任何想法都会非常有帮助。不幸的是不能使用 JavaScript。

<div class="tablenoborder">
    <table cellpadding="4" cellspacing="0" summary="" class="table" frame="border" border="1" rules="all">
        <tbody class="tbody">
            <tr class="row">
                <td class="entry" valign="top">
                    <strong class="ph b">Option</strong>
                </td>
                <td class="entry" valign="top">
                    <strong class="ph b">Description</strong>
                </td>
            </tr>
        </tbody>
    </table>
</div>

<div class="tablenoborder">
    <table cellpadding="4" cellspacing="0" summary="" class="table" frame="border" border="1" rules="all">
        <thead class="thead" align="left">
            <tr class="row">
                <th class="entry" valign="top" id="d29429e118">UI Elements</th>
                <th class="entry" valign="top" id="d29429e121">Description</th>
            </tr>
        </thead>
        <tbody class="tbody">
            <tr class="row">
                <td class="entry" valign="top" headers="d29429e118 ">Language</td>
                <td></td>
            </tr>
        </tbody>
    </table>
</div>

最佳答案

试试这个选择器:

.table > :first-child > tr:first-child > * {
    /* ... */
}

:first-child 将同时匹配 theadtbody 并且您可以从那里选择 tr 的直接子代th(在 thead 内)或 td(在 tbody 内)的元素。

查看下面的演示。

.table > :first-child > tr:first-child > * {
    background: #EEE;
}

.tablenoborder {margin-bottom: 10px;}
<div class="tablenoborder">
    <table cellpadding="4" cellspacing="0" summary="" class="table" frame="border" border="1" rules="all">
        <tbody class="tbody">
            <tr class="row">
                <td class="entry" valign="top">
                    <strong class="ph b">Option</strong>
                </td>
                <td class="entry" valign="top">
                    <strong class="ph b">Description</strong>
                </td>
            </tr>
            <tr class="row">
                <td class="entry" valign="top">
                    <strong class="ph b">Option</strong>
                </td>
                <td class="entry" valign="top">
                    <strong class="ph b">Description</strong>
                </td>
            </tr>
        </tbody>
    </table>
</div>

<div class="tablenoborder">
    <table cellpadding="4" cellspacing="0" summary="" class="table" frame="border" border="1" rules="all">
        <thead class="thead" align="left">
            <tr class="row">
                <th class="entry" valign="top" id="d29429e118">UI Elements</th>
                <th class="entry" valign="top" id="d29429e121">Description</th>
            </tr>
        </thead>
        <tbody class="tbody">
            <tr class="row">
                <td class="entry" valign="top" headers="d29429e118 ">Language</td>
                <td></td>
            </tr>
        </tbody>
    </table>
</div>

关于html - 在 table.th.tr 和 table.tbody.tr 中为两种类型的表选择 th,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29730856/

相关文章:

javascript - 网页长列表项印象

javascript - Bootbox 对话框模态 Bootstrap 4

javascript - Gallery Filters,如何在这个例子中使用?

html - <hr> 标签中间有 'OR'

python - 如何解析表格中的行,这些行不仅由 <td> 单元格组成,而且偶尔还由 <th> 单元格组成?

html - 为什么边框 Prop 不起作用?

jquery - HTML5弹出框

javascript - javascript 上的 setTimeout() 用于基于表单提交的自定义 URL

html - 设置 flex 行高以适合子 img 大小

javascript - KnockoutJS 呈现具有多个模板和可变列数的表