是否允许 figcaption 成为 HTML5 中 <a> 标签的子标签?

Is figcaption allowed to be a child of an <a> tag in HTML5?

我认为在 HTML5 中你可以将块元素作为 <a> 元素的子元素,正如规范所理解的那样:

https://www.w3.org/TR/html-markup/a.html#a

Although previous versions of HTML restricted the a element to only containing phrasing content (essentially, what was in previous versions referred to as “inline” content), the a element is now transparent; that is, an instance of the a element is now allowed to also contain flow content (essentially, what was in previous versions referred to as “block” content)—if the parent element of that instance of the a element is an element that is allowed to contain flow content.

但是现在,当我使用 HTML 验证器检查我的页面时,我发现了这个错误消息:

Error: Element “figcaption” not allowed as child of element “a” in this context. (Suppressing further errors from this subtree.)

代码如下:

<figure class="post">
  <a href="#" title="foo">
    <figcaption class="articuloInfo ">
      <h3>FOO</h3>
      <p class="fecha">4/04/2014</p>
      <div class="descripcion">
      </div>
    </figcaption>
    <div class="imagen">
      <img src="foo.jpg" alt="foo">
    </div>
  </a>
</figure>

谁能告诉我错误在哪里以及为什么?

一个图形标题只能有图形元素作为它的 parent:

https://developer.mozilla.org/en/docs/Web/HTML/Element/figcaption

因此图形标题不能直接 child 锚标签。

你不应该使用 https://www.w3.org/TR/html-markup/ because it’s outdated (it’s a Working Group Note from 2013). The HTML5 specification is: https://www.w3.org/TR/2014/REC-html5-20141028/

对于 figcaption element,规格列表 "Contexts in which this element can be used",它们是:

As the first or last child of a figure element.

它说 child(不是 descendant),所以它不能有一个 a 元素作为父元素。

您可以将 a 元素放在 figure 元素周围(这是可能的,因为您引用的部分:a 现在可以包含流内容):

<a href="#" title="foo">
  <figure class="post">
    <!-- … -->
  </figure>
</a>