表格行可以没有单元格吗? [英] Can a table row have no cells?
问题描述
我正在使用HTML表格来创建时间表。每行代表半小时,单元格可以使用 rowspan
跨越多行。
I'm using HTML tables to create a timetable. Each row represents half hour, and the cells can span multiple rows by using rowspan
.
有时我有空 tr
元素,因为前一行的单元格都占用了所有的插槽。
Sometimes I have empty tr
elements, because all the slots are taken by cells from previous rows.
在这种情况下, HTML验证器投诉:
行由
tbody
元素建立的行组的[N]从它开始没有单元格
。
Row [N] of a row group established by a
tbody
element has no cells beginning on it.
当然,我可以删除那些空的 tr
并减少 rowspan
的值扩大的细胞。但那么 rowspan
值将不再单一地对应于单元格的持续时间。
Sure, I could remove those empty tr
and reduce the rowspan
value of the expanded cells. But then the rowspan
value would no longer univocally correspond to the duration of the cell.
因此,为空 tr
s真的无效?为什么?
Therefore, are empty tr
s really invalid? Why?
推荐答案
根据当前的HTML规范,有效HTML文档中的表中不能有空行。
You can’t have empty rows in a table in a valid HTML document per the current HTML spec.
也许你正确应该可以,但规范目前明确表示你不能。因此,如果有人认为规范应该允许它,那么正确的做法是针对HTML提出问题标准的github跟踪器甚至编写补丁并为其打开公关。
Maybe you rightly should be able to, but the spec currently clearly says that you can’t. So if anybody believes it should be allowed by the spec, the right thing to do is to file an issue against the HTML Standard in its github tracker or even write a patch and open a PR for it.
具体来说,规范定义了以下错误情况:
Specifically the spec defines the following error case:
https://html.spec.whatwg.org/multipage/tables.html#the-table-element
如果表中只有一个行或列包含没有锚定到它们的单元格的插槽,那么这是一个表模型错误。
If there exists a row or column in the table containing only slots that do not have a cell anchored to them, then this is a table model error.
在Internet规范术语中,这是一个规范权威声明,不能被忽略或覆盖还要别的吗。它表示硬性要求。
In Internet-spec terms, that is a "normative" authoritative statement that can’t be ignored or overridden by anything else. It is stating a hard requirement.
其他地方的规格说明:
tr
元素:内容模型
The
tr
element: Content model
https://html.spec.whatwg.org/multipage/tables.html #the-tr-element
零或多个td,th和脚本支持元素
https://html.spec.whatwg.org/multipage/tables.html#the-tr-element Zero or more td, th, and script-supporting elements
但是这实际上不是矛盾,与上面引用的表模型错误要求不冲突或取代或覆盖。事实上,正好相反 - 表格模型错误要求取代了另一部分中更为宽松的要求(上文引用)有效 tr
元素可以有零个或多个子元素。
But that’s not actually a contradiction and does not conflict with or supersede or override the "table model error" requirement cited above. In fact, it’s the opposite—the "table model error" requirement supersedes the more-liberal requirement in the other section (cited above) that a valid tr
element can have zero or more children.
规范中任何更严格的要求总是取代或覆盖任何更自由的要求。
Any stricter requirements in a spec always supersede or override any more-liberal requirements.
这篇关于表格行可以没有单元格吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!