我无法在HTML标记内插入注释的原因?

时间:2022-09-22 15:08:06

Are there any reason why I can't insert a comment inside the HTML tag?

我有什么理由不在HTML标签中插入注释吗?

Sample: In HTML (Not Possible)

示例:在HTML中(不可能)

 <img <!-- sample comment--> src="" alt="Sample Picture" class="img-circle center-block" />

While in JavaScript it can be easily done.

在JavaScript中,它可以很容易地完成。

Sample: In JS (Possible)

示例:在JS中(可能)

verticalCentered: /* Sample Comment*/ true

Please explain it in a detailed way.

请详细解释一下。

4 个解决方案

#1


5  

HTML was originally defined formally as an application of SGML, and the comment syntax was taken from SGML. SGML uses two hyphens -- as comment delimiters, but it permits comments in certain contexts only. Of these contexts, only a comment declaration is relevant in HTML. (Well, in theory they are also allowed in entity declarations like <!ENTITY foo "foo" -- Comment -- >, but entity declarations were never implemented in any browser before XHTML, and in XHTML, following XML syntax, entity declarations cannot contain comments.)

HTML最初被正式定义为SGML的应用程序,注释语法来自SGML。 SGML使用两个连字符 - 作为注释分隔符,但它仅允许在某些上下文中进行注释。在这些上下文中,只有注释声明与HTML相关。 (嗯,理论上它们也被允许在实体声明中,如 ,但实体声明从未在XHTML之前的任何浏览器中实现,而在XHTML中,遵循XML语法,实体声明不能包含评论。)

Thus, comments are only allowed in comment declarations that contain nothing but comments, e.g.

因此,注释仅允许在注释声明中包含除注释之外的任何注释,例如注释。

<!-- Comment one -- -- Comment two -->

Browsers actually implemented this partly in a simplified (wrong) way, so the practical recommendation is to have comments only in comment declarations that contain a single comment:

浏览器实际上部分地以简化(错误)方式实现了这一点,因此实际建议仅在包含单个注释的注释声明中添加注释:

<!-- Comment -->

Comment declarations are at the same structural level as tags and therefore cannot appear within tags. (Comments cannot contain tags either: anything that would otherwise constitute a tag, like <p>, is taken just as character data when inside a comment.)

注释声明与标记处于相同的结构级别,因此不能出现在标记内。 (注释也不能包含标签:否则构成标签的任何内容,如

,在注释中都会被视为字符数据。)

This has not been changed in later HTML versions. They have simplified the syntax in matters like this, not extended.

这在以后的HTML版本中没有改变。他们简化了这方面的语法,而不是扩展。

#2


4  

If you are using a templating language, you may be able to use the templating language's comment mechanism to do what you want: <img {{! this is a comment }} src=.... This also has the advantage that the comment will be removed at compile time, and thus won't litter your HTML with comments.

如果您使用的是模板语言,您可以使用模板语言的注释机制来执行您想要的操作:我无法在HTML标记内插入注释的原因?

#3


-2  

Because comment is also a tag & you can not write html tag inside anther html tag.

因为注释也是一个标签,你不能在anther html标签内写html标签。

#4


-6  

You can insert a comment into html tag. If im using a netbeans then you can disable HTML error checking when the error appears

您可以在html标记中插入注释。如果即时通讯使用netbeans,则可以在出现错误时禁用HTML错误检查

#1


5  

HTML was originally defined formally as an application of SGML, and the comment syntax was taken from SGML. SGML uses two hyphens -- as comment delimiters, but it permits comments in certain contexts only. Of these contexts, only a comment declaration is relevant in HTML. (Well, in theory they are also allowed in entity declarations like <!ENTITY foo "foo" -- Comment -- >, but entity declarations were never implemented in any browser before XHTML, and in XHTML, following XML syntax, entity declarations cannot contain comments.)

HTML最初被正式定义为SGML的应用程序,注释语法来自SGML。 SGML使用两个连字符 - 作为注释分隔符,但它仅允许在某些上下文中进行注释。在这些上下文中,只有注释声明与HTML相关。 (嗯,理论上它们也被允许在实体声明中,如 ,但实体声明从未在XHTML之前的任何浏览器中实现,而在XHTML中,遵循XML语法,实体声明不能包含评论。)

Thus, comments are only allowed in comment declarations that contain nothing but comments, e.g.

因此,注释仅允许在注释声明中包含除注释之外的任何注释,例如注释。

<!-- Comment one -- -- Comment two -->

Browsers actually implemented this partly in a simplified (wrong) way, so the practical recommendation is to have comments only in comment declarations that contain a single comment:

浏览器实际上部分地以简化(错误)方式实现了这一点,因此实际建议仅在包含单个注释的注释声明中添加注释:

<!-- Comment -->

Comment declarations are at the same structural level as tags and therefore cannot appear within tags. (Comments cannot contain tags either: anything that would otherwise constitute a tag, like <p>, is taken just as character data when inside a comment.)

注释声明与标记处于相同的结构级别,因此不能出现在标记内。 (注释也不能包含标签:否则构成标签的任何内容,如

,在注释中都会被视为字符数据。)

This has not been changed in later HTML versions. They have simplified the syntax in matters like this, not extended.

这在以后的HTML版本中没有改变。他们简化了这方面的语法,而不是扩展。

#2


4  

If you are using a templating language, you may be able to use the templating language's comment mechanism to do what you want: <img {{! this is a comment }} src=.... This also has the advantage that the comment will be removed at compile time, and thus won't litter your HTML with comments.

如果您使用的是模板语言,您可以使用模板语言的注释机制来执行您想要的操作:我无法在HTML标记内插入注释的原因?

#3


-2  

Because comment is also a tag & you can not write html tag inside anther html tag.

因为注释也是一个标签,你不能在anther html标签内写html标签。

#4


-6  

You can insert a comment into html tag. If im using a netbeans then you can disable HTML error checking when the error appears

您可以在html标记中插入注释。如果即时通讯使用netbeans,则可以在出现错误时禁用HTML错误检查