我试图验证以下HTML代码
<!DOCTYPE html>
<html>
<head>
<title>hello</title>
</head>
<body>
<p>
This is some <b>text</b>
<ul>
<li>A bullet point</li>
<li>another bullet point</li>
</ul>
</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
它按预期呈现,但Validator会引发以下错误:
在我看来,有一个合适的对<p>(...)</p>,为什么错误呢?
查看DOM检查器实际呈现的方式.你会看到类似的东西:
<p>...</p>
<ul>...</ul>
Run Code Online (Sandbox Code Playgroud)
甲ul元件不能嵌套一个内部p元件.存在<ul>隐含地关闭了<p>那一点.因此尾随</p>没有上下文.
官方规范中的推理是:
p
允许的内容:措辞内容.
https://www.w3.org/TR/html-markup/p.html
ul
允许的父元素:可以包含流元素的任何元素.
https://www.w3.org/TR/html-markup/ul.html