Why does the HTML validator give me an error about this HTML comment?

江枫思渺然 提交于 2021-01-28 05:15:54

问题


This is just question about comments inside of an HTML page.

For example:

<html>
    <head></head>
    <body>
    <!--
    <p><font size="4">--</font></p>
    -->
    </body>
</html>

Using http://validator.w3.org/check I get the following error message. Does it make sense?

Line 6, Column 21: invalid comment declaration: found character "<" outside comment but inside comment declaration

<p><font size="4">--</font></p>

Check that you are using a proper syntax for your comments, e.g: . This error may appear if you forget the last "--" to close one comment, and later open another.


回答1:


Yes, it makes sense.

To understand the error message, you have to understand the comment tag. It consists of three key parts, the <! that starts the tag, the -- that starts and ends the comment, and the > that ends the tag.

When you put -- inside the text of a comment, you actually end the comment part. The rest of the text until the end of the tag is invalid, as it's not a comment, and you can only have comments inside the comment tag.

You can also have more than one comment in the tag:

<!-- comment -- -- another comment -->



回答2:


Looks like the string -- isn’t allowed within HTML comments.




回答3:


No, it does intuitively not make sense to most of the logical thinking humans, but the validator has it really right. The -- denotes a comment delimiter. Also see W3 validator bug 5555:

yes, really invalid. -- is a comment delimiter in XML.




回答4:


I think it's being confused by the two hyphens in the middle. The ones in >--<.

If you must have those, perhaps you could substitute the escape character &#45; for them.



来源:https://stackoverflow.com/questions/3320350/why-does-the-html-validator-give-me-an-error-about-this-html-comment

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!