HTML5 是否禁止 tbody 中的单元格?

发布于 2024-10-28 08:37:24 字数 835 浏览 3 评论 0原文

我将以下标记作为 Razor 视图的一部分:

<table>
  <caption>Presidents</caption>
  <thead>
    <tr>
      <th scope="col">Name</th>
      <th scope="col">Born</th>
      <th scope="col">Died</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">Washington</th>
      <td>1732</td>
      <td>1799</td>
    </tr>
    <!-- etc -->
  </tbody>
</table>

将“验证的目标架构”设置为 HTML5 时,Visual Studio 会这样抱怨:

警告 1 验证 (HTML5):元素“th”不得嵌套在元素“tbody tfoot”内。

这是真的吗?如果是这样,有人可以链接到该规范吗?

我的理解是,使用 作为行标题不仅合法,而且受到鼓励。这看起来确实很常见,我可以链接数十个教程(看起来很明智)解释它有助于提高可访问性。

这是VS的bug吗? HTML5 带来了真正的变化(好的变化?坏的变化?)?这是什么故事?

I have the following markup as a part of a Razor view:

<table>
  <caption>Presidents</caption>
  <thead>
    <tr>
      <th scope="col">Name</th>
      <th scope="col">Born</th>
      <th scope="col">Died</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">Washington</th>
      <td>1732</td>
      <td>1799</td>
    </tr>
    <!-- etc -->
  </tbody>
</table>

With the "target schema for validation" set to HTML5, Visual Studio complains thusly:

Warning 1 Validation (HTML5): Element 'th' must not be nested within element 'tbody tfoot'.

Is this really true? If so, could someone link to the spec?

My understanding was that using <th> for row headers was not just legal but encouraged. It certainly seems fairly common, I could link dozens of tutorials explaining (seemingly sensibly) that it helps with accessibility.

Is this a VS bug? A real change coming with HTML5 (a good one? a bad one?)? What's the story?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瞳孔里扚悲伤 2024-11-04 08:37:24

我的理解是,使用 作为行标题不仅合法,而且受到鼓励

据我所知,这在 HTML 4(可能还有其前身)中始终是合法的,并且没有改变在 HTML5 中。

W3C 的 HTML5 验证器虽然仍处于实验阶段,但没有报告任何警告或错误。话又说回来,我确信 Visual Studio 使用的 HTML5 验证也是实验性的,因为 HTML5 本身尚未最终确定。

关于标记表格数据的 HTML5 规范,特别是 第 4.9.13 节,显示了使用 中的 来限定行数据范围:

<table>
 <thead>
  <tr>
   <th>
   <th>2008
   <th>2007
   <th>2006
 <tbody>
  <tr>
   <th>Net sales
   <td>$ 32,479
   <td>$ 24,006
   <td>$ 19,315
  <tr>
   <th>Cost of sales
   <td>  21,334
   <td>  15,852
   <td>  13,717
 <tbody>
  <tr>
   <th>Gross margin
   <td>$ 11,145
   <td>$  8,154
   <td>$  5,598
 <tfoot>
  <tr>
   <th>Gross margin percentage
   <td>34.3%
   <td>34.0%
   <td>29.0%
</table>

因此,使用 < 是完全合法的 内的 元素内的 ;th> 元素。无论如何,表格标题应该如此,因为表格标题不仅仅出现在表格标题上。

My understanding was that using <th> for row headers was not just legal but encouraged

As far as I know, this was always legal in HTML 4 (and possibly its predecessors), and hasn't changed in HTML5.

W3C's HTML5 validator, while still experimental, reports no warnings or errors. Then again, I'm sure the HTML5 validation Visual Studio is using is experimental as well since HTML5 itself hasn't yet been finalized.

The HTML5 spec on marking up tabular data, specifically section 4.9.13, shows the use of <th> within <tbody> and <tfoot> to scope row data:

<table>
 <thead>
  <tr>
   <th>
   <th>2008
   <th>2007
   <th>2006
 <tbody>
  <tr>
   <th>Net sales
   <td>$ 32,479
   <td>$ 24,006
   <td>$ 19,315
  <tr>
   <th>Cost of sales
   <td>  21,334
   <td>  15,852
   <td>  13,717
 <tbody>
  <tr>
   <th>Gross margin
   <td>$ 11,145
   <td>$  8,154
   <td>$  5,598
 <tfoot>
  <tr>
   <th>Gross margin percentage
   <td>34.3%
   <td>34.0%
   <td>29.0%
</table>

So it's perfectly legitimate to have <th> elements inside <tr> elements inside either a <tbody> or <tfoot>. As it should be anyway, since table headings aren't just found on table headers.

七堇年 2024-11-04 08:37:24

HTML5 规范 仅需要它位于 tr 内,并且规范实际上包含一个 th 嵌套在 tbody 内的示例。

The HTML5 spec only requires that it be inside a tr, and the spec actually includes an example with a th nested inside a tbody.

指尖上的星空 2024-11-04 08:37:24

通常,THEAD 中的 THscope 值为 "col",而 TH< TBODY 中的 /code> 的 scope 值为 “row”

Generally a TH in a THEAD will have a scope value of "col" while a TH in a TBODY will have a scope value of "row".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文