HTML - 评论

Comment是一段代码,任何Web浏览器都会忽略它.最好在HTML代码中添加注释,特别是在复杂文档中,以指示文档的各个部分,以及任何查看代码的人的任何其他注释.评论可以帮助您和其他人理解您的代码并提高代码的可读性.

HTML评论放在<! -  ...  - > 标签之间.因此,使用<! -  ...  - >放置的任何内容标记将被视为注释,浏览器将完全忽略它们.

示例

<!DOCTYPE html>
<html>

   <head>  <!-- Document Header Starts -->
      <title>This is document title</title>
   </head> <!-- Document Header Ends -->
	
   <body>
      <p>Document content goes here.....</p>
   </body>
	
</html>

这将产生以下结果,而不显示作为评论的一部分给出的内容 :

Valid vs Invalid Comments

注释不会嵌套,这意味着不能将注释放入另一个注释中。 其次,双破折号序列"-"可能不会出现在注释中,除非作为结束->标记的一部分。 您还必须确保注释开头的字符串中没有空格。

Example

在此,给定的注释是有效的注释,将被浏览器擦除。

<!DOCTYPE html>
<html>

   <head>
      <title>Valid Comment Example</title>
   </head>
	
   <body>
      <!--   This is valid comment -->
      <p>Document content goes here.....</p>
   </body>
	
</html>

这将产生以下结果 :

但是,下一行不是有效的注释,将由浏览器显示。 这是因为在左尖括号和感叹号之间有一个空格。

<!DOCTYPE html>
<html>

   <head>  
      <title>Invalid Comment Example</title>
   </head>
	
   <body>
      < !--   This is not a valid comment -->
      <p>Document content goes here.....</p>
   </body>
	
</html>

This will produce the following result &minus;

Multiline Comments

到目前为止,我们已经看到了单行注释,但是HTML也支持多行注释。

您可以通过特殊的开始标记<!-和结束标记->注释多行,如下文中的示例所示,该标记放在第一行和最后一行的末尾。

Example

<!DOCTYPE html>
<html>

   <head>  
      <title>Multiline Comments</title>
   </head> 
	
   <body>
      <!-- 
         This is a multiline comment and it can
         span through as many as lines you like.
      -->
      
      <p>Document content goes here.....</p>
   </body>
	
</html>

Conditional Comments

条件注释仅在Windows上的Internet Explorer(IE)中有效,但其他浏览器将忽略它们。 从Explorer 5开始支持它们,您可以使用它们为IE的不同版本提供条件说明。

Example

<!DOCTYPE html>
<html>

   <head>  
      <title>Conditional Comments</title>

      <!--[if IE 6]>
         Special instructions for IE 6 here
      <![endif]-->
   </head> 
   
   <body>
      <p>Document content goes here.....</p>
   </body>
	
</html>

您会遇到需要根据Internet Explorer的不同版本应用不同的样式表的情况,在这种情况下,条件注释会很有帮助。

Using Comment Tag

很少有浏览器支持<comment>标记来注释HTML代码的一部分。

注意:<comment>标记在HTML5中已弃用。 不要使用此元素。

Example

<!DOCTYPE html>
<html>

   <head>
      <title>Using Comment Tag</title>
   </head>
	
   <body>
      <p>This is <comment>not</comment> Internet Explorer.</p>
   </body>
	
</html>

如果使用的是IE,则会产生以下结果:

但是,如果您不使用IE,则会产生以下结果:

Commenting Script Code

尽管您将学习HTML的JavaScript,但是在单独的教程中,但是必须在此处进行说明,如果您在HTML代码中使用Java Script或VB Script,则建议将该脚本代码放入适当的HTML注释中,以便 浏览器可以正常工作。

Example

<!DOCTYPE html>
<html>

   <head>
      <title>Commenting Script Code</title>
      
      <script>
         <!-- 
            document.write("Hello World!")
         //-->
      </script>
   </head>
	
   <body>
      <p>Hello , World!</p>
   </body>
	
</html>

Commenting Style Sheets

尽管您将在单独的教程中学习如何将样式表与HTML结合使用,但是在此处必须注意,如果您在HTML代码中使用级联样式表(CSS),则建议将该样式表代码放入适当的HTML注释中 这样旧的浏览器才能正常运行。

Example

<!DOCTYPE html>
<html>

   <head>
      <title>Commenting Style Sheets</title>
      
      <style>
         <!--
            .example {
               border:1px solid #4a7d49;
            }
         //-->
      </style>
   </head>
	
   <body>
      <div class = "example">Hello , World!</div>
   </body>
	
</html>