meta、link 和 style 元素在文档的 body 中有什么用途?

What purpose do the meta, link and style elements have within a document's body?

HTML4.01 规范说得很清楚:

Unlike a, [the link element] may only appear in the head section of a document...

HTML4.01 Specification - 12.3 Document relationships: the link element

它还接着说了关于 metastyle 元素的大致相同的事情。

但是 HTML5 规范指出:

A body element's start tag may be omitted if the element is empty, or if the first thing inside the body element is not a space character or a comment, except if the first thing inside the body element is a meta, link, script, style, or template element.

HTML5 Specification - 4.3.1 The body element

似乎没有继续解释为什么 metalinkstyle 元素突然被允许包含在 body 元素中;特别不是作为第一个元素。

它确实给出了几个 stylelink 元素的用法示例,但并没有很好地解释它们。例如,一个以 link 元素为特色的示例给出了这个:

<header>
  <h1 itemprop="headline">The Very First Rule of Life</h1>
  <p><time itemprop="datePublished" datetime="2009-10-09">3 days ago</time></p>
  <link itemprop="url" href="?comments=0">
</header>

它没有解释为什么在这里使用 link 元素而不是 a - 似乎定义 itemprop 属性的微数据工作组注释也没有解释。

这是怎么回事?为什么这些元素突然被允许在 body 中(并且作为第一个 child 有点重要)?

It doesn't appear to go on to explain why the meta, link and style elements are suddenly allowed to be contained within the body element

这似乎是一些未纳入 HTML 5.

的提案遗留下来的

请参阅 HTML 5.1 which allows those elements in flow content 并扩展它们的定义以描述它们出现在 body 中时的作用。

especially not as the very first element.

作为第一个元素的意义只与标签省略规则有关。

鉴于:

<head …>
    <title>
<h1>

很明显h1结束了head元素,开始了body元素,因为h1不能出现在head中

鉴于:

<head …>
    <title>
<meta>
<h1>

您不能将相同的规则应用于 <meta>,因为它可能出现在 的 body.

开头

对于微数据和 RDFa,使用 linkmeta 元素代替默认情况下具有可见内容的元素会很有用。 (这就是为什么两个规格 extend HTML5 so that link/meta may be used in the body。)

示例:

  • 您想提供一个 represents the thing 您编写的 URI。

    虽然当有人请求这个 URI 时提供一个关于这个东西的网页是一个很好的做法,但这不是必需的:URI http://example.com/my-vocabulary/Berlin 可以代表柏林市,而不提供任何类型的内容。为此设置 visible/clickable link 是没有意义的。

    如果您使用为网页提供服务的词汇表,情况也是如此,但让您的人类用户访问它们没有用。在 DBpedia 中,柏林的 URI 是 http://dbpedia.org/resource/Berlin. If you visit it, you get redirected to http://dbpedia.org/page/Berlin(这个 URI 不代表城市,而是一个关于城市的页面),但是这个页面对你的访问者来说不一定有用。

  • 您必须以特定格式提供内容。

    Schema.org 的 openingHours property 期望内容的格式类似于 "Tu,Th 16:00-20:00"。您不太可能希望它完全像这样在您的页面上可见(如果您不使用英语,则更不可能)。

  • 保留值。

    Schema.org 的 accessibilityHazard property 可以有一个像 "noFlashingHazard" 这样的值,这在页面上可见并不是很有用。

    Schema.org 的 availableDeliveryMethod property 可以有像 http://purl.org/goodrelations/v1#DeliveryModeFreight 这样的 URI 作为值,这可能对您的用户没有用。

  • 可用性。

    有时您希望为机器人(如搜索引擎)提供更多可能对您的人类访问者有用的数据,但您不希望它在页面上可见(例如,为了美观或可用性)原因)。

    例如,您可能有预告片图片,其中每个预告片都 linked 到关于此图片的页面。每张图片都有一个作者,但您不想 link 显示预告片的作者简介,只希望在专用页面上显示。但是通过使用 link,您已经可以 link 来自预告片页面的作者,从而为消费者提供更多 data/context.

    在包含的标记片段中,目的可能是以首选形式(使用此 为事物(即当前博客 post)提供 link comments 参数)没有 "confusing" 用户通过跟随 link 进入同一页面。