html 表格:tad vs th

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5395228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:31:10  来源:igfitidea点击:

html tables: thead vs th

htmlhtml-table

提问by chris

It looks like (according to the examples on this page, anyways) that if you're using THEAD, you don't need to use TH.

看起来(无论如何,根据本页上的示例),如果您使用的是 THEAD,则不需要使用 TH。

Is that true? If so, what are the advantages/disadvantages of THEAD vs TH?

真的吗?如果是这样,THEAD 与 TH 的优缺点是什么?

采纳答案by sgokhales

The <thead>tag is used to group the header content in an HTML table. The theadelement should be used in conjunction with the tbodyand tfootelements.

<thead>标签用于对 HTML 表中的标题内容进行分组。该thead元素应与tbodytfoot元素结合使用。

More : thead

更多:THEAD

You use <thead>to encapsulate an entire row (or rows) to designate them as the Table Header. According to the spec,

您用于<thead>封装整行(或多行)以将它们指定为表头。根据规范,

"This division enables user agents to support scrolling of table bodies independently of the table head and foot. When long tables are printed, the table head and foot information may be repeated on each page that contains table data."

“这种划分使用户代理能够支持独立于表格头和脚的表格主体的滚动。当打印长表格时,表格头和脚信息可能会在包含表格数据的每个页面上重复。”

<th>, on the other hand, is used to style a specific cell as a header cell rather than an ordinary data cell.

<th>,另一方面,用于将特定单元格设置为标题单元格而不是普通数据单元格。

回答by rafr3

<th>actually is a replacement for <td>when you want to mark a cell as a header cell.

<th>实际上是将<td>单元格标记为标题单元格时的替代品。

If you want to use <thead>and <th>don't forget to nest <th>inside <tr>. Otherwise the code may not be valid.
Example:

如果你想使用<thead><th>不要忘记嵌套<th><tr>. 否则代码可能无效。
例子:

<table>
  <thead>
    <tr>
      <th>Season</th>
      <th>Goals</th>
      <th>Assists</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2009-2010</th>
      <td>25</td>
      <td>43</td>
    </tr>
    <tr>
      <th>2011-2012</th>
      <td>40</td>
      <td>20</td>
    </tr>
  </tbody>
</table>

回答by Nick Manning

this more specific than what may reside inside of thead. A thcell is to specify the header of the corresponding tdcells. In fact you can add a headersattribute to a tdcell which points to the id of a thcell (for screen readers). So this directly related to the tds of that column.

th比可能驻留在thead. 一个th单元格是指定对应td单元格的标题。事实上,您可以headerstd单元格添加一个属性,该属性指向单元格的 id th(对于屏幕阅读器)。Sothtd该列的s直接相关。

However, theadcan include any information...commonly yes it does include the thcells but it can also include anything that you might deem to be appropriate as information at the top of the table (other than a caption, because this has its own tag as well).

但是,thead可以包含任何信息……通常是的,它确实包含th单元格,但它也可以包含您认为适合作为表格顶部信息的任何内容(标题除外,因为它有自己的标记为好)。

回答by smartrahat

<thead>

<thead>

Table rows may be grouped into a table head, table foot, and one or more table body sections, using the THEAD, TFOOTand TBODYelements, respectively. This division enables user agents to support scrolling of table bodies independently of the table head and foot. When long tables are printed, the table head and foot information may be repeated on each page that contains table data.

可以分别使用THEADTFOOTTBODY元素将表格行分组为表格头、表格脚和一个或多个表格主体部分。这种划分使用户代理能够支持独立于表头和表脚的表体滚动。打印长表时,表头和表脚信息可能会在包含表数据的每一页上重复。

The table head and table foot should contain information about the table's columns. The table body should contain rows of table data.

表头和表脚应包含有关表列的信息。表体应包含表数据行。

When present, each THEAD, TFOOT, and TBODY contains a row group. Each row group must contain at least one row, defined by the TR element.

当存在时,每个 THEAD、TFOOT 和 TBODY 都包含一个行组。每个行组必须至少包含一行,由 TR 元素定义。

<th>

<th>

Table cells may contain two types of information: header information and data. This distinction enables user agents to render header and data cells distinctly, even in the absence of style sheets. For example, visual user agents may present header cell text with a bold font. Speech synthesizers may render header information with a distinct voice inflection.

表格单元格可能包含两种类型的信息:标题信息和数据。这种区别使用户代理能够清楚地呈现标题和数据单元格,即使在没有样式表的情况下也是如此。例如,可视化用户代理可以用粗体显示标题单元格文本。语音合成器可以用不同的语音变化来呈现标题信息。

The TH element defines a cell that contains header information. User agents have two pieces of header information available: the contents of the TH element and the value of the abbr attribute. User agents must render either the contents of the cell or the value of the abbr attribute. For visual media, the latter may be appropriate when there is insufficient space to render the full contents of the cell. For non-visual media abbr may be used as an abbreviation for table headers when these are rendered along with the contents of the cells to which they apply.

TH 元素定义包含标题信息的单元格。用户代理有两条可用的头信息:TH 元素的内容和 abbr 属性的值。用户代理必须呈现单元格的内容或 abbr 属性的值。对于视觉媒体,当没有足够的空间来呈现单元格的全部内容时,后者可能是合适的。对于非视觉媒体,当表格标题与其应用的单元格的内容一起呈现时,abbr 可用作表格标题的缩写。

Source: http://www.w3.org/TR/html4/struct/tables.html

来源:http: //www.w3.org/TR/html4/struct/tables.html

回答by Diodeus - James MacFarlane

<thead>is special in that it can be used to repeat the header row at the top of the page in printed versions.

<thead>特殊之处在于它可用于在打印版本中重复页面顶部的标题行。

回答by amphetamachine

As far as I can tell from experience, there is no difference in rendering unless you're using CSS to specify a difference in rendering. A <td>inside of a <thead>will render the same as a <th>inside of a <table>or a <tbody>.

根据我的经验,除非您使用 CSS 来指定渲染的差异,否则渲染没有区别。a 的<td>内部<thead>将呈现与<th>a<table>或 a的内部相同<tbody>

回答by DanMan

There are no hard rules here. The <thead>element is just another way to group your columns and rows, just like <tbody>and <tfoot>is. So you have more possibilities for scripting and formatting.

这里没有硬性规定。该<thead>元素是只是另一种方式将你的行和列,就像<tbody><tfoot>是。所以你有更多的可能性来编写脚本和格式化。