为什么嵌套在p中的表单不验证为XHTML
收藏

以这个XHTML代码段为例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>A webpage</title>
</head>
<body>
  <p>
    <form action="something.php" method="get">
      <input type="submit" value="Hello"/>
    </form>
  </p>
</body>
</html>

该树应该有效,但是在浏览器中无法正确解析。

最佳答案

查看当您尝试使用http://validator.w3.org时收到的错误消息

Apart from a warning that you haven't specified a character encoding (and that it's therefore assuming UTF-8), the main error is that a <p> isn't allowed to contain non-inline content. You can either remove the <p> and </p> completely, or, move them inside the <form>.

As for 'why', it's because that's how it's defined in the schema which defines what is and what is not valid XHTML. If you look at this section of the XHTML definition you'll see that <p> is only allowed to contain text or 'inline' (not 'block') tags. However a <form> counts as 'block' content not as 'inline' content.

换句话说,表单可以包含段落,但是段落不能包含表格。

    公众号
    关注公众号订阅更多技术干货!