Table of Contents
定义
用法
浏览器的支持情况" >浏览器的支持情况
高级应用
1、分配工作" >1、分配工作
2、说明功能模块及使用方法
3、对于页面制作的备注" >3、对于页面制作的备注
4、在页内CSS/JS的作用
5、利用注释插入代码
6、利用条件注释来兼容各浏览器
条件注释html标签
根据注释引用不同的样式表
根据条件加载js
BUG问题
IE6下的小尾巴
注释出现在DOCTYPE之前
前端其它注释
css中的注释
js中的注释
总结
Home Web Front-end HTML Tutorial 聊一聊HTML <!--…-->标签

聊一聊HTML <!--…-->标签

Jun 01, 2016 pm 02:32 PM

定义

注释标签用于在html源代码中插入注释。注释不会在浏览器上显示。

用法

根据定义的基本用法,代码如下

<!-- 这是一段注释,我不会显示在页面上 -->
Copy after login

浏览器的支持情况

所有浏览器都支持
上面这些只是最简单的定义和使用方法。对于注释的使用,有哪些更高级的使用。

高级应用

1、分配工作

<!-- todo:张三 begin -->
<div></div>
<!-- end -->
Copy after login

2、说明功能模块及使用方法

<!-- 激活状态类:active,默认状态类:default,无法使用状态类:disabled -->
<ul><li class="active">激活</li><li class="default">默认</li><li class="disabled">无法使用</li></ul>
Copy after login

3、对于页面制作的备注

<!-- 此页面创建于2016/5/31,前端:李四,设计:王五 -->
Copy after login

以上3种应用都是基于注释的说明作用,利用其在浏览器上不显示的特性,对文档进行说明,方便工作中跨组跨部门的沟通。

4、在页内CSS/JS的作用

用于兼容老版的浏览器,对于js和css还无法识别。(只用于了解,现在基本可以放弃使用这个功能了),代码如下

<!DOCTYPE html>
<html>
<head>
<style>
.good{color:red;}
</style>
</head>
<body>
<script>alert('good')</script>
<p>morning</p>
</body>
</html>
Copy after login

上面的代码会直接显示在页面

.good{color:red;}alert('good')
morning
Copy after login

可以使用注释,以防止代码显示,影响页面体验。

<!DOCTYPE html>
<html>
<head>
<style>
<!--
.good{color:red;}
-->
</style>
</head>
<body>
<script>
<!--
alert('good')
//-->
</script>
<p>morning</p>
</body>
</html>
Copy after login

此处理还使用了//,是javascript的注释符号,防止js对-->执行。

5、利用注释插入代码

这点也是利用注释不会被页面渲染的特性,来对页面进行优化。可以用来存储数据,可以用来存储模板。

<!-- {id:10000,type:ad,image:1.jpg} -->
<div is-tpl>
<!--
<div>1111111</div>
<div>2222222</div>
-->
</div>
Copy after login

这里的代码可以用正则表达进行匹配,来取出对应数据然后对数据进行解析处理。下面是使用jquery简单的演示代码

$('[is-tpl]').each(function(){
	console.log($(this).html())	
	var comment=$(this).html();
	$(this).html(comment.replace('<!--','').replace('-->',''));
})
Copy after login

这样代码也正确显示了,具体注释里要放什么数据以及怎么处理,大家自己思考一下吧。

6、利用条件注释来兼容各浏览器

以下条件注释判断IE浏览器(IE10以后已经不支持这些注释了),其它浏览器都会认为下面是注释,不进行解析。

<!--[if IE 6]> 仅IE6可识别 <![endif]-->
<!--[if IE]> 所有的IE可识别 <![endif]--> 
<!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--> 
<!--[if IE 6]> 仅IE6可识别 <![endif]--> 
<!--[if lt IE 6]> IE6以下版本可识别 <![endif]--> 
<!--[if gte IE 6]> IE6以及IE6以上版本可识别 <![endif]--> 
<!--[if IE 7]> 仅IE7可识别 <![endif]--> 
<!--[if lt IE 7]> IE7以下版本可识别 <![endif]--> 
<!--[if gte IE 7]> IE7以及IE7以上版本可识别 <![endif]--> 
<!--[if IE 8]> 仅IE8可识别 <![endif]--> 
<!--[if IE 9]> 仅IE9可识别 <![endif]-->
Copy after login

以上代码大家应该是比较熟悉的,只做js和移动端的高大上,应该没有见过。

看着很多,记忆这个可以根据下面的规律记:
a、基本结构:

<!--[if ]> 代码 <![endif]--> 
Copy after login

b、和IE关系

  • 等于为空
  • 大于为gt
  • 小于为lt
  • 大小于加等于gte,lte

c、然后加版本号
d、最后记得中间用空格
记住一个特别的其他浏览器使用的。

<!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--> 
Copy after login

这个可以解释成,两对注释加中间代码。

对于这些个条件注释的使用,经常看到有:

条件注释html标签

<!DOCTYPE html>
<!--[if IE 6 ]> <html class="ie6 lte_ie6 lte_ie7 lte_ie8" lang="zh-CN"> <![endif]-->
<!--[if lte IE 6 ]> <html class="lte_ie6 lte_ie7 lte_ie8" lang="zh-CN"> <![endif]-->
<!--[if lte IE 7 ]> <html class="lte_ie7 lte_ie8" lang="zh-CN"> <![endif]-->
<!--[if lte IE 8 ]> <html class="lte_ie8" lang="zh-CN"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="zh-CN"><!--<![endif]-->
Copy after login

这个可以在特定的浏览器去对样式进行定义

.ie6 body{}
Copy after login

上面这个只有在ie6的浏览器才会有这个样式。

根据注释引用不同的样式表

<!--[if IE 6 ]>
<link rel="stylesheet" type="text/css" media="all" href="./ie6.css" />
<![endif]-->
Copy after login

根据条件加载js

在IE6,引用对png24的图像支持的js

<!--[if IE 6]> 
<script src="DD_belatedPNG.js" mce_src="DD_belatedPNG.js"></script> 
<script type="text/javascript"> 
DD_belatedPNG.fix('.png');// .png改成使用了透明PNG图片的选择器 
</script> 
<![endif]--> 
Copy after login

对于DD_belatedPNG.js,大家百度一下,很容易找到相关的使用方法和库。

BUG问题

IE6下的小尾巴

在ie6下,对于行内元素,中间添加注释,可能会产生,一个尾巴。

<div style="width:80px; background:red;"><!--我是一个注释--><a href="#">1</a><a href="#">12</a><a href="#">31</a><!--我是一个注释--><a href="#">41</a><a href="#">51</a><a href="#">61</a><!--我是一个注释--><a href="#">71asdfasfd</a><!--我是一个注释--></div>
<style>
a{
float:left;
display:inline-block;
padding:0 3px;
}
</style>
Copy after login

如图

image
只要删除各行注释和换行,问题解决。

注释出现在DOCTYPE之前

虽然说DOCTYPE html之前不应该出现任何代码,但出现注释,各主流浏览器并不会出现问题。但在ie7及以下浏览器会无法识别渲染类型,导致使用怪异模式渲染,出现页面样式错乱。

<!--我是第一行的注释-->
<!doctype html>
<html>
<head>
<meta charset="gb2312">
<title>无标题文档</title>
<style>
.demo{
	width:100px;
	margin:0 auto;
	height:100px;
	background:red;
}
</style>
</head>

<body>
<div class="demo"></div>
</body>
</html>
Copy after login

ie7及以下显示为

image

前端其它注释

css中的注释

/* 我是css中的注释 */
Copy after login

css中的注释,只有这一种。

注意:使用中文注释注意在注释符号的前后各加一个空格,防止编码错误乱码导致样式无法读取。

js中的注释

单行注释

//我是单行注释
Copy after login

多行注释

/* 我是多行注释
我是多行注释
*/
Copy after login

建议都使用多行注释,以防止出现,换行符删除后出现的代码功能错误。

var s=10;//定义了s为10
var b=20;
console.log(b);
Copy after login

少了换行符后

var s=10;//定义了s为10var b=20;
console.log(b);
Copy after login

这时出现错误。
如果使用的多行则

var s=10;/*定义了s为10*/var b=20;
console.log(b);
Copy after login

代码不会出错。

以上情况也会出现在多个js合并时,单行注释也会造成相应的错误。

总结

通过这一通查找资料,一直也没觉得一个注释标签可以整理这么多东西。里面有些比如建议或注意的问题,都是我在实际工作中遇到过的。其他没有接触的也无从写起了,还有什么漏掉的,可以在评论中给我一些建议。当然希望你对这个文章喜欢,并关注我。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

See all articles