【问题标题】:Is the insertion of <tbody> in HTML tables standard? [duplicate]在 HTML 表格中插入 <tbody> 是标准的吗? [复制]
【发布时间】:2015-06-16 14:17:52
【问题描述】:

在 HTML 中,当解析 table 元素内部带有 tr 元素时,通常会插入 tbody

function log(el, ctx) {
  var li = document.createElement('li');
  li.appendChild(document.createTextNode(el.tagName.toLowerCase()));
  var ul = document.createElement('ul');
  for(var i=0; i<el.children.length; ++i)
    log(el.children[i], ul);
  li.appendChild(ul);
  ctx.appendChild(li);
}
log(document.querySelector('table'), document.getElementById('dom-tree'));
table {
  display: none;
}
<table>
  <tr><td>1</td></tr>
  <tr><td>2</td></tr>
</table>
The table is parsed as:
<ul id="dom-tree"></ul>

这是行为标准吗?

我在the spec 中找不到它的描述,但它是有道理的,因为insertRow DOM 方法也会自动创建tbody 元素:

tr = 表格insertRow( [ index ] )

创建一个tr 元素,以及一个tbody(如果需要),插入它们 在参数给定的位置进入表中,并返回 tr.

【问题讨论】:

  • tbody 是标准 HTML 定义的一部分(请参阅您为规范提供的链接中的第 4.9.5 节),但不需要使用它。这样渲染引擎就可以区分标题和正文(参见&lt;thead&gt;)。
  • @lurker 是的,我知道tbody 不是必需的(table 的内容模型允许tr 没有tbody)。但是,如果您不包含它,大多数浏览器都会插入它。我可以依赖它吗?
  • @lurker 您应该将其发布为答案
  • @lurker 我已经包含了一个 sn-p,它表明当表被解析到 DOM 时,tbody字面上插入的。这发生在 Firefox、Chrome 和 IE8 上。
  • 啊,谢谢你的sn-p。我明白你的意思了。对我来说,除非它更改您的 HTML 文件,否则它不是字面意思。除此之外,它仍然是“内部”渲染。 ;) 语义差异... :)

标签: html html-parsing html-table language-lawyer


【解决方案1】:

是的,这是标准的。它在Parsing HTML documents(而不是Tabular data)中描述:

如果 nodetable 元素,则将insertion mode 切换为“in table

8.2.5.4.9 The "in table" insertion mode

当用户代理要应用“in table”的规则时 insertion mode,用户代理必须按如下方式处理令牌:

【讨论】:

    猜你喜欢
    • 2015-02-08
    • 1970-01-01
    • 2015-11-21
    • 2013-01-18
    • 2011-06-19
    • 2016-03-19
    • 1970-01-01
    • 2021-08-21
    • 2021-04-26
    相关资源
    最近更新 更多