有效的 HTML 和 TD 元素的数量
我有一个包含几列和几行的 HTML 表格。 最后一行并不总是具有与其他行一样多的 TD 元素。 它在我检查过的几个浏览器中正确呈现并通过了验证检查。 但这似乎是不正确的。 每行中 TD 元素的数量应该相同吗?
编辑:无论有或没有 colspans 和 rowspans 都可能发生这种情况。
I have a HTML table with several columns and several rows. The last row does not always have as many TD elements as the other rows. It renders correctly in several browsers I have checked and passes validation checks. But it seems incorrect. Should the number of TD elements be the same in every row?
EDIT: This could happen with or without colspans and rowspans.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该规范明确允许行之间的列数不一致< /a> (至少在标记中,浏览器应该生成额外的标记来弥补差异)。
…但是,坦率地说,这样的数据结构很少有任何意义。 因此,如果行长度不一致,那么您可能做错了什么。
The specification explicitly allows for inconsistent numbers of columns between rows (in markup at least, the browser is expected to generate extra ones to make up the difference).
… but, frankly, it's very rare that such a data structure would make any sense at all. So if you have inconsistent row lengths, then you are probably doing something wrong.
“每一行的 TD 元素数量应该相同吗?”
虽然这不是严格要求的,但我说“是的,或者你应该有 colspan 来补偿它们。”
尽管 W3C 规范没有要求,但我对此的看法是,当考虑到浏览器支持任何内容的方式参差不齐时,我会使用 colspans 或空单元格使其尽可能一致,以确保它在所有浏览器上都完全符合我的要求。
第二个好处是对于维护代码的任何人来说。 使意图更加明显,而不是被视为可能的错误。
"Should the number of TD elements be the same in every row?"
Though it is not strictly required, I say "yes, or you should have colspans to compensate for them."
My take on this, despite that it is not required by the W3C spec, is that when considering how spotty browsers are about the way they support anything, I'd make it as consistent as possible, using colspans or empty cells, to make sure that it's doing exactly what I want on all browsers.
The secondary benefit is for anyone maintaining your code. Makes it more obvious what is intended, rather than it being percieved as a possible error.
HTML 标准中不要求每行定义的列数匹配。 可以说,从道德或最佳实践的角度来看,您“应该”提供统一数量的列,我支持这一点,但标准并不要求这样做,这就是它验证的原因。
There is no requirement in HTML standards that the number of columns defined by each row match. It can be said that you "should" provide a uniform number of columns, from a moralistic or best-practices standpoint, and I'd support that, but it isn't required by standard, which is why it's validating.
只是一个想法:尝试使用元素创建表格;-) 渲染表格时存在一些问题。
Just an idea: try to create your table with element ;-) There are some issues in rendering tables.