我的页面只包含以下 HTML 代码:
<article>
<nav>
<a href="test.html" rel="parent">Back</a>
</nav>
<header>
<h2>Test</h2>
</header>
<p>Test</p>
</article>
当我尝试 validate it 时为什么会收到此警告?
Warning: Article lacks heading. Consider using
h2
-h6
elements to add identifying headings to all articles.From line 10, column 2; to line 10, column 10
<body>↩↩↩
<article>
↩ <na
最佳答案
原因是分段内容元素(在您的情况下为 nav
)位于标题内容元素(在您的情况下为 h2
)之前:
<!-- A -->
<article>
<nav></nav>
<h2>Test</h2>
</article>
如果你切换它们,验证器不会报告警告:
<!-- B -->
<article>
<h2>Test</h2>
<nav></nav>
</article>
查看这些片段生成的文档概述部分:
<!-- A -->
- article (no heading)
-- nav (no heading)
- "Test" (h2 of an implied section, on the same level as the article)
<!-- B -->
- "Test" (h2 of article)
-- nav (no heading)
规范引用
Creating an outline描述算法。我认为相关的步骤是这一步(但我不习惯阅读此类算法,所以最好持保留态度):
When entering a sectioning content element
Run these steps:
If
current outline owner
is not null, run these substeps:
- If the
current section
has no heading, create an implied heading and let that be the heading for thecurrent section
.
据我了解,这就是示例“A”所发生的情况:
- 算法输入一个分段内容元素(
article
)。 - 在它找到一个标题(
h2
)之前,它找到另一个分段内容元素(nav
),并立即输入它,所以引用的步骤适用。 文章
仍然是当前大纲所有者
和当前部分
,因此子步骤适用(因为当前大纲所有者
不为空)。- 由于
current section
(→article
) 没有标题(它还没有找到h2
),隐含的标题是创建。所以article
有一个标题,但它不是你的h2
。
我认为规范也可以在文本 ( I was surprised by this, too) 中理想地描述这一点。
验证器问题跟踪器中的相关帖子:Warning issued when headers are nested in an article
关于html - w3c 验证警告 : Article lacks heading,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50706583/