我正在使用 HTML 表格来创建时间表。每行代表半小时,单元格可以使用 rowspan
跨越多行。
有时我有空的 tr
元素,因为所有的槽都被前几行的单元格占用了。
在这种情况下,HTML validator提示:
Row [N] of a row group established by a
tbody
element has no cells beginning on it.
当然,我可以删除那些空的 tr
并减少展开单元格的 rowspan
值。但是 rowspan
值将不再明确对应于单元格的持续时间。
因此,空的 tr
真的无效吗?为什么?
最佳答案
根据当前的 HTML 规范,在有效的 HTML 文档中,表格中不能有空行。
也许你正确地应该能够,但规范目前明确表示你不能。因此,如果有人认为规范应该允许它,那么正确的做法是 file an issue against the HTML Standard in its github tracker甚至 write a patch and open a PR for it .
具体来说,规范定义了以下错误情况:
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 规范术语中,这是一个“规范”的权威声明,不能被任何其他内容忽略或覆盖。它说明了一个硬性要求。
别处的规范是这样说的:
The
tr
element: Content modelhttps://html.spec.whatwg.org/multipage/tables.html#the-tr-element Zero or more td, th, and script-supporting elements
但这实际上并不矛盾,不会与上面引用的“表模型错误”要求冲突,也不会取代或覆盖。事实上,它恰恰相反——“表模型错误”要求取代了其他部分中更自由的要求(上面引用)有效的 tr
元素可以有零或更多 child 。
规范中任何更严格的要求总是取代或覆盖任何更自由的要求。
关于html - 表格行可以没有单元格吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32382485/