Why do internal TABLE sections have to go THEAD TF

2020-06-07 05:06发布

问题:

I often use THEAD, TBODY, and TFOOT elements to divide my data tables into sections that can be addressed separately with CSS. I also understand that there is always an implicit TBODY tag.

What puzzles me is the order that these have to go in to validate. THIS table will validate:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Table Validation Test</title>
</head>
<body>

<table>

<thead>
<tr>
    <th scope="col">Enemies List</th>
</tr>
</thead>

<tfoot>
<tr>
    <td>&copy; Bomb Voyage</td>
</tr>
</tfoot>

<tbody>
<tr>
    <td>Mr. Incredible</td>
    <td>Elastigirl</td>
    <td>Gazer Beam</td>
</tr>
</tbody>

</table>
</body>
</html>

But this one will not:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Table Validation Test</title>
</head>
<body>

<table>

<thead>
<tr>
    <th scope="col">Enemies List</th>
</tr>
</thead>

<tbody>
<tr>
    <td>Mr. Incredible</td>
    <td>Elastigirl</td>
    <td>Gazer Beam</td>
</tr>
</tbody>


<tfoot>
<tr>
    <td>&copy; Bomb Voyage</td>
</tr>
</tfoot>

</table>
</body>
</html>

The valid one goes HEAD, FOOT, BODY; which does not make any sense.

Putting the foot at the bottom of the table would maintain the analogy between the table and a human body. But for some reason, this order is considered invalid.

Anyone know why?

回答1:

The spec provides a reason:

TFOOT must appear before TBODY within a TABLE definition so that user agents can render the foot before receiving all of the (potentially numerous) rows of data.

http://www.w3.org/TR/html401/struct/tables.html#h-11.2.3

I don't know if any browsers actually follow this behavior, and it was changed in HTML5 to handle both the HTML 4 order and the more logical order:

In this order: optionally a caption element, followed by zero or more colgroup elements, followed optionally by a thead element, followed optionally by a tfoot element, followed by either zero or more tbody elements or one or more tr elements, followed optionally by a tfoot element (but there can only be one tfoot element child in total).

http://www.w3.org/TR/html5/tabular-data.html