Html 简明教程

HTML - Comments

HTML 注释用于对 HTML 代码进行注释,以便开发人员可以理解该代码部分的用途,它还有助于调试。如果我们因为某个特定元素而遇到一些问题,我们可以通过注释掉该元素来检查它。

HTML Comments are used to comment in HTML codes, so the developer can understand the purpose of that code section and it is helpfull for debugging also. If we are facing some issue becouse of any particular element we cah check it by commenting out that element.

What are HTML Comments?

  • HTML comment tag* 用于创建注释。HTML 中的注释就像给自己和其他人的一个注释。注释完全会被 Web 浏览器忽略,因此它们不会影响页面的外观或工作方式。HTML 中有两种类型的注释。

HTML comment tag is used to create a comment. A comment in HTML is like a note for yourself and others. Comments are completely ignored by web browsers, so they don’t affect how your page looks or works. There are two types of comment in HTML.

  1. Single Line Comment: The single-line comment is given inside the <!-- …​ -→.

  2. Multi Line Comment: Same as single-line comment but if we add multiple lines in the comment we will have multi-line comments.

Why to use HTML Comments?

注释可以帮助您和其他人理解代码并提高代码的可读性。HTML 注释放在 <!-- …​ -→ 标签之间。因此,放在 <!-- …​ -→ 标签内的任何内容都将被视为注释,并且浏览器将完全忽略它。

Comments help you and others to understand the code and increases code readability. HTML comments are placed in between <!-- …​ -→ tags. So, any content placed with-in <!-- …​ -→ tags will be treated as comment and will be completely ignored by the browser.

尝试单击图标运行按钮,以运行以下 HTML 代码并查看输出。

Try to click the icon run button to run the following HTML code to see the output.

Examples of HTML Comments

在下面的示例中,我们将看到两种类型的注释 singlemultiline

In the bellow examples we will witness single and multiline both types of comments.

HTML Single Line Comment

在此示例中,我们将为 body 标签中使用的每个元素使用单行注释。

Here in this example we will use single line comment for each element used in the body tag.

<!DOCTYPE html>
<html>
<head>
    <title>Online HTML Editor</title>
</head>
<body>
    <!-- This is a single Comment-->
    <h1>Tutorialspoint</h1>
    <!-- This is a single line Commnet -->
    <p>Simply Easy Learning</p>
</body>
</html>

HTML Multi Line Comment

在此示例中,我们将为 body 标签中使用的每个元素使用多行注释。

Here in this example we will use multi line comment for each element used in the body tag.

<!DOCTYPE html>
<html>
<head>
    <title>Online HTML Editor</title>
</head>
<body>
    <!--
       This is a multiline Comment
       Heading Section
    -->
    <h1>Tutorialspoint</h1>
    <!--
       This is a multiline line Commnet
       paragrapgh Scetion
    -->
    <p>Simply Easy Learning</p>
</body>
</html>

Hide Content using HTML Comment

当我们需要调试 HTML 代码时,这是必需的,这将帮助我们不渲染特定的 html 元素。

This is required when we will work on debugging HTML code, this will help us to not to render specific html element.

<!DOCTYPE html>
<html>
<head>
    <title>Hiding p Element</title>
</head>
<body>
    <h1>Tutorialspoint</h1>
    <!--  <p>Simply Easy Learning</p> -->
</body>
</html>

Valid vs Invalid Comments

HTML 中的注释有一个你需要遵循的规则。你需要确保注释字符串的开始或结束没有空格。

Comments in HTML have a single that you need to follow. You need make sure that there are no spaces in the start or end of comment string.

<!DOCTYPE html>
<html>
<head>
   <title>Valid & Invalid Comment</title>
</head>
<body>
   <!--   This is valid comment -->
   < !--   This is not a valid comment -->
   < !--   This is not a valid comment -- >
</body>
</html>

Conditional Comments

条件注释是 Windows 上 Internet Explorer (IE) 独有的功能,但其他浏览器会忽略这些注释。它们自 Explorer 5 版本起得到支持,你可以使用它们向 IE 的不同版本提供条件指令。

Conditional comments are a feature specific to Internet Explorer (IE) on Windows but they are ignored by other browsers. They are supported from Explorer 5 onwards, and you can use them to give conditional instructions to different versions of IE.

<!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>