为什么 <content> 标签必须用开始和结束标签来定义?

Why the <content> tag must be defined with a starting and ending tag?

我在 Firefox/Chrome 中使用了这两种语法来定义 WebComponent。

打开标签

<content></content>

关闭标签

<content/>

并且都按预期工作。

以第一种方式定义 <content> 的(技术)原因是什么?


MDN Content element 表示开始和结束标记是 强制性的 ,但未指定原因。

您实际上必须使用 <content></content> 形式。在正常的 HTML(非 XML)文档(这是您想要在此处制作的内容)中,使用 HTML 解析在所有浏览器引擎中工作的标准方式,<content/> 标签是 不是 一个 content 结束标签——它只是一个 content 开始标签,里面有一个额外的 / 只是被忽略了通过 HTML 个解析器。

在你的情况下,这意味着当 HTML 解析器没有看到任何 </content> 结束标记 content-start-tag-with-extra-/-最后,解析器 从未实际关闭 content 元素 ,因此 *所有跟随 content 元素的节点都成为它的子内容.

要明白我的意思,请查看 DOM view of what the <content/> looks like to an HTML parser

MDN Content element indicates that the empty tag form is mandatory, but doesn't specify why.

我不确定您所说的“空标签表单”是什么意思。该 MDN 页面说:

Tag omission: None, both the starting and ending tag are mandatory.

这意味着 content 元素 必须 有一个结束标记。