HTML 中的单行注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2640453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
single line comment in HTML
提问by aslum
Is there a way to comment out a single line in HTML using just an escape sequence at the start of the line?
Similar to using #
or //
in other languages? Or is <!-- ... -->
the only option for commenting in html?
有没有办法只在行的开头使用转义序列来注释 HTML 中的一行?类似于使用#
或//
在其他语言中?或者是<!-- ... -->
在 html 中评论的唯一选择?
回答by digitaldreamer
from http://htmlhelp.com/reference/wilbur/misc/comment.html
来自http://htmlhelp.com/reference/wilbur/misc/comment.html
Since HTML is officially an SGML application, the comment syntax used in HTML documents is actually the SGML comment syntax. Unfortunately this syntax is a bit unclear at first.
The definition of an SGML comment is basically as follows:
A comment declarationstarts withThis means that the following are all legal SGML comments:<!
, followed by zero or more comments, followed by>
. A commentstarts and ends with "--
", and does not contain any occurrence of "--
".Note that an "empty" comment tag, with just "
<!-- Hello -->
<!-- Hello -- -- Hello-->
<!---->
<!------ Hello -->
<!>
--
" characters, should always have a multiple of four "-
" characters to be legal. (And yes,<!>
is also a legal comment - it's the empty comment).Not all HTML parsers get this right. For example, "
<!------> hello-->
" is a legal comment, as you can verify with the rule above. It is a comment tag with two comments; the first is empty and the second one contains "> hello". If you try it in a browser, you will find that the text is displayed on screen.There are two possible reasons for this:
There is also the problem with the "
- The browser sees the ">" character and thinks the comment ends there.
- The browser sees the "
-->
" text and thinks the comment ends there.--
" sequence. Some people have a habit of using things like "<!-------------->
" as separators in their source. Unfortunately, in most cases, the number of "-
" characters is not a multiple of four. This means that a browser who triesto get it right will actually get it wronghere and actually hide the rest of the document.For this reason, use the following simple rule to compose valid and accepted comments:
An HTML comment begins with "<!--
", ends with "-->
" and does not contain "--
" or ">
" anywhere in the comment.
回答by Matti Virkkunen
No, <!-- ... -->
is the only comment syntax in HTML.
不,<!-- ... -->
是 HTML 中唯一的注释语法。
回答by Ahmad Awais
Let's keep it simple. Loved @digitaldreamer 's answer but it might leave the beginners confused. So, I am going to try and simplify it.
让我们保持简单。喜欢@digitaldreamer 的回答,但它可能会让初学者感到困惑。所以,我将尝试简化它。
The only HTML comment is <!-- -->
It can be used as a single line comment or double, it is really up to the developer.
唯一的 HTML 注释是<!-- -->
它可以用作单行注释或双行注释,这真的取决于开发人员。
So, an HTML comment starts with <!--
and ends with -->
. It is really that simple. You should not use any other format, to avoid any compatibility issue even if the comment format is legit or not.
因此,HTML 注释<!--
以-->
. 真的就是这么简单。您不应使用任何其他格式,以避免任何兼容性问题,即使注释格式合法与否。
回答by Kyle Alons
No, you have to close the comment with -->.
不,您必须使用 --> 关闭评论。
回答by anonymous
TL;DR For conforming browsers, yes; but there are no conforming browsers, so no.
TL;DR 对于符合标准的浏览器,是的;但是没有符合标准的浏览器,所以没有。
According to the HTML 4 specification, <!------> hello-->
is a perfectly valid comment. However, I've not found a browser which implements this correctly (i.e. per the specification) due to developers not knowing, nor following, the standards (as digitaldreamer pointed out).
根据 HTML 4 规范,<!------> hello-->
是一个完全有效的注释。但是,由于开发人员不了解或不遵循标准(如 digitaldreamer 指出的那样),我还没有找到正确实现这一点的浏览器(即根据规范)。
You can find the definition of a comment for HTML4 on the w3c's website: http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.4
您可以在 w3c 的网站上找到 HTML4 注释的定义:http: //www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.4
Another thing that many browsers get wrong is that -- >
closes a comment just like -->
.
许多浏览器出错的另一件事是-- >
关闭评论就像-->
.