在带引号的字符串中使用 <html> 的 W3C 验证错误:'document type does not allow element "html" here'

W3C validation error for use of <html> in quoted string: 'document type does not allow element "html" here'

我在引用字符串中使用 HTML 时收到以下错误:

Error Line 63, Column 39: document type does not allow element "html" here

mywindow.document.write("<html><head><title>mydiv</title>");

The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed).

是我做错了什么还是 W3C 验证器给出了错误的结果?

您不会在 HTML 文档中遇到该错误。

据推测,您正在写 XHTML.

我的第一条建议是:不要。 XHTML 通常比它的价值更麻烦。 改用HTML 5 不会无效.


如果你需要使用 XHTML 然后查看规范:

与HTML的区别:Script and Style elements

In XHTML, the script and style elements are declared as having #PCDATA content. As a result, < and & will be treated as the start of markup, and entities such as &lt; and &amp; will be recognized as entity references by the XML processor to < and & respectively. Wrapping the content of the script or style element within a CDATA marked section avoids the expansion of these entities.

<script type="text/javascript">
<![CDATA[
... unescaped script content ...
]]>
</script>

HTML 兼容性准则:Embedded Style Sheets and Scripts:

Use external style sheets if your style sheet uses < or & or ]]> or > --. Use external scripts if your script uses < or & or ]]> or --. Note that XML parsers are permitted to silently remove the contents of comments. Therefore, the historical practice of "hiding" scripts and style sheets within "comments" to make the documents backward compatible is likely to not work as expected in XML-based user agents.