简体   繁体   English

为什么W3C验证服务说这不是有效的XHTML?

[英]Why does the W3C Validation Service say this is not valid XHTML?

Can someone tell me why the W3C Validation Service says that this code is not valid? 有人可以告诉我为什么W3C验证服务说此代码无效吗?

<!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>this is the title of the web page</title>
</head>
<body>

<form action="formscript.php" method="post">
    first name:<input type="text" name="firstname" />

    <input type="submit" />
</form>

</body>
</html> 

I tried both codes and the validator says that the question code is valid as XHTML 1.0 Transitional. 我尝试了两种代码,验证器说问题代码作为XHTML 1.0 Transitional有效。

Anyway, the problem here is that you have no <fieldset> inside your <form> and that the text <input> has no <label> . 无论如何,这里的问题是<form>没有<fieldset> <form>且文本<input>没有<label>

<form action="formscript.php" method="post" name="thisform">
  <fieldset>
    <label for="firstname">first name:</label>
    <input type="text" name="firstname" />

    <input type="submit" />
  </fieldset>
</form>

And don't forget the DOCTYPE . 并且不要忘记DOCTYPE

The markup, as originally posted, makes a valid XHTML 1.0 Transitional document if the XHTML 1.0 Transitional document type declaration is added at the start. 如果最初在XHTML 1.0 Transitional文档类型声明中添加了该标记,则该标记将构成有效的XHTML 1.0 Transitional文档。

As edited by @adamjansch, with the XHTML 1.0 Strict document type declaration, it is not valid, and presumably it was validated using that document type declaration. 正如@adamjansch编辑的那样,对于XHTML 1.0 Strict文档类型声明,它无效,并且大概已使用该文档类型声明进行了验证。 Then the first error message is “character data is not allowed here” with some explanations of possible causes, including “putting text directly in the body of the document without wrapping it in a container element (such as a <p>aragraph</p> )”. 然后第一个错误消息是“此处不允许使用字符数据”,并对可能的原因进行了一些解释,包括“将文本直接放入文档主体而不将其包装在容器元素中(例如<p>aragraph</p> )”。 This gets close: the problem is that in XHTML 1.0 Strict, a form element may have block-level children only. 这就接近了:问题是在XHTML 1.0 Strict中, form元素可能仅具有块级子级。

This means that any text and text-level markup like input must be wrapped in one or more block containers, such as p , div , fieldset , or table . 这意味着任何文本和文本级别的标记(如input必须包装在一个或多个块容器中,例如pdivfieldsettable Of these, div is the only neutral one, with no default impact on rendering: 其中, div是唯一的中性对象,对渲染没有默认影响:

<!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>this is the title of the web page</title>
</head>
<body>

<form action="formscript.php" method="post">
  <div>
    first name:<input type="text" name="firstname" />

    <input type="submit" />
  </div>
</form>

</body>
</html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM