如何在HTML语言中添加注释

下载PDF文件下载PDF文件

写完代码后做个注释,方便日后再编辑页面时有个提示或解释。如果一个新功能尚不成熟,需要不断测试、反复编辑,这时加个注释,马上就能禁用部分代码。学学如何正确加注释,肯定会对编写源代码大有好处。

  1. How.com.vn 中文: Step 1 插入单行注释。
    注释部分要写在<!---->这一对标签内。在代码某处插入一个简短的注释,以后就能提醒自己这段代码到底是什么情况。
    <html><head><title>Comment test</title></head><body><!-- This code makes a paragraph --><p>This is the website</p></body></html>
    • 注释的标签本身不能有空格。例如,如果< !--这个标签中间出现空格,就不能激活注释功能。但是对开始标签和结束标签之间的内容没限制,随便加多少空格都可以。
  2. How.com.vn 中文: Step 2 实现多行注释。
    注释也可以分成多行,多行注释对解释复杂代码或屏蔽大段代码大有用处。
    <html><head><title>Comment test</title></head><body><!-- 注释多长都行。注释标签之间的内容对页面代码没有影响。--><p>This is the website</p></body></html>
  3. How.com.vn 中文: Step 3 用注释功能快速禁用代码。
    如果你想找出程序中的bug,或者想阻止代码在页面运行,用注释功能马上就能达到目的。之后,只要删除注释标签,代码就能恢复原样,就这么简单。
    <html><head><title>Comment test</title></head><body><p>Check out these images</p><img src="https://zh.wikihow.com/images/image1.jpg"><!-- Hiding this image for now<img src="https://zh.wikihow.com/images/image2.jpg">--></body></html>
  4. How.com.vn 中文: Step 4 用注释功能在不支持有关脚本程序的浏览器隐藏脚本。
    在用JavaScript或VBScript编程时,添加注释功能可在不支持脚本的浏览器隐藏脚本。为了确保脚本程序在支持它们的浏览器正常运行,脚本一开始插入注释,并以//-->结束注释。
    <html><head><title>VBScript</title></head><body><script language="vbscript" type="text/vbscript"><!--document.write("Hello World!")//--></script></body></html>
    [1]
    • 结束标签处有//,如果浏览器支持该脚本文件,这个标记会防止脚本文件自动执行注释功能。
    广告

小提示

  • 注释一般是为了方便日后查找代码,让人对程序整个运行过程有个了解。
广告

关于本How.com.vn

How.com.vn是一个“多人协作写作系统”,因此我们的很多文章都是由多位作者共同创作的。 为了创作这篇文章,16位用户(部分匿名)多次对文章进行了编辑和改进。 这篇文章已经被读过9,490次。
分类: 计算机
本页面已经被访问过9,490次。

这篇文章对你有帮助吗?

⚠️ Disclaimer:

Content from Wiki How 中文 language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

Notices:
  • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
  • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
  • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
  • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

广告