是否可以嵌套 HTML 注释?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/442786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:59:16  来源:igfitidea点击:

Are nested HTML comments possible?

htmlvalidationcomments

提问by QAZ

as per the title; is it possible to have nested comments in valid HTML? see the example below...

按照标题;是否可以在有效的 HTML 中嵌套注释?看下面的例子...

<p>some text</p>

  <!-- comment 1

    <p>commented out html</p>

    <!-- comment 2

      // are nested html comment allowed?

    end of comment 2 -->

    <p>more commented out html</p>

  end of comment 1 -->

<p>some more text</p>

It appears not, does anybody know how I could get nested comments to work?

似乎不是,有人知道我如何让嵌套评论起作用吗?

回答by Aaron Digulla

When you nest a comment, replace "--" with "- -". When you un-nest, reverse the procedure. It's not the <!--that is forbidden but the --.

嵌套注释时,将“--”替换为“- -”。取消嵌套时,请反转该过程。不是<!--被禁止的,而是被禁止的--

Example:

例子:

<!-- some stuff
<!- - some inner stuff - ->
<!- - a sibling - ->
the footer -->

回答by Dave Land

TL;DR: Unfortunately, no, it's not possible (and never will be).

TL;DR:不幸的是,不,这是不可能的(而且永远不会)。

Short answer:

简短的回答:

An HTML comment is not quite what many think it is. HTML is a form of SGML, in which comments are delimited by pairs of double-dashes (--…?--).

HTML 注释并不像许多人认为的那样。HTML 是 SGML 的一种形式,其中注释由成对的双破折号 ( --...? --)分隔。

Thus, any pair of a double-dashes inside a pair of angle brackets with an exclamation point after the opening bracket (<!--? -->) is a comment. The spec says it better than I can: http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.4

因此,一对尖括号内的任何一对双破折号在左括号 ( <!--? -->)之后带有感叹号都是注释。规范说得比我好:http: //www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.4

This is why comments like this one (which we've alllikely done one time or another) are a badidea:

这就是为什么像这样的(这我们的意见可能做一个时间或其他)是一个主意:

<!-- ------------------ HEADER BEGINS HERE -------------------- -->

Truth: I am too lazy to tell you how many comments are represented by the above tag pollution, but it's at least 10.

真相:我懒得告诉你上面的标签污染代表了多少条评论,但至少有10条。

I got less lazy: This so-called "comment" actually consists of 10 comments, three words outside any comment (i.e., just bad SGML), and the beginning of a comment that is not terminated. It's a real mess:

<!--1----2----3----4----5--
HEADER BEGINS HERE
--6----7----8----9----10-- -->

我变得不那么懒了:这个所谓的“评论”实际上由 10 条评论、任何评论之外的三个词(即,只是糟糕的 SGML)和未终止的评论的开头组成。真是一团糟:

<!--1----2----3----4----5--
HEADER BEGINS HERE
--6----7----8----9----10-- -->

Of course, it's not quitethat simple, due to differences in how each browser chooses to interpret the spec.

当然,这不是简单,因为在每个浏览器选择如何解释规范的差异。

Here's an excellent article that explains it:

这是一篇很好的文章,解释了它:

????http://weblog.200ok.com.au/2008/01/dashing-into-trouble-why-html-comments.html

???http://weblog.200ok.com.au/2008/01/dashing-into-trouble-why-html-comments.html

Long answer: Why we get it wrong

长答案:为什么我们弄错了

Most of us who grew up with HTML (without delving into the SGML that underlies it)) have come to believe that the string <!--begins a comment, and the string -->ends a comment.

大多数在 HTML 中长大的人(没有深入研究它背后的 SGML))已经开始相信字符串<!--开始注释,字符串-->结束注释。

Actually, <!and >delimit an SGML declaration within your HTML document, such as the DOCTYPE declaration we've all seen at the top of our pages. Withinan SGML declaration, comments are delimited by double-dashes. Thus, the HTML comment

其实,<!>界定你的HTML文档内的SGML声明,如DOCTYPE声明我们都看到在我们的网页上。SGML 声明中,注释由双破折号分隔。因此,HTML 注释

<!-- this is a comment -->

which most of us would believe is parsed like this <!--this is a comment -->is actually parsed like this:
<!--this is a comment-->. It is an SGML declaration that is empty except for a comment.

我们大多数人会相信被解析这样<!--this is a comment -->实际上是解析如下:
<!--this is a comment-->。它是一个 SGML 声明,除了注释外都是空的。

Because HTML is a form of SGML, this "comment-within-a-declaration" functions as an HTML comment.

因为 HTML 是 SGML 的一种形式,所以这个“声明内的注释”起到 HTML 注释的作用。

Out of interest, here's a chunk of pure SGML that shows comments functioning as they were intended in SGML: this attribute list definition contains a comment on each line:

出于兴趣,这里有一大块纯 SGML,它显示了在 SGML 中按预期运行的注释:此属性列表定义在每一行都包含一个注释:

<!ATTLIST LINK
  %attrs;                              -- %coreattrs, %i18n, %events --
  charset     %Charset;      #IMPLIED  -- char encoding of linked resource --
  href        %URI;          #IMPLIED  -- URI for linked resource --
  hreflang    %LanguageCode; #IMPLIED  -- language code --
  type        %ContentType;  #IMPLIED  -- advisory content type --
  rel         %LinkTypes;    #IMPLIED  -- forward link types --
  rev         %LinkTypes;    #IMPLIED  -- reverse link types --
  media       %MediaDesc;    #IMPLIED  -- for rendering on these media --
>

回答by Sergio

It cannot be done. -->will always end an existing HTML comment.

这是不可能的。-->将始终结束现有的 HTML 注释。

回答by Mathijs Flietstra

If you're really stuck with some piece of HTML – pre-rendered at some uncontrollable source – which contains comments, and you need to make sure none of it is rendered on your page, you can always wrap it with a scripttag like below, only thing is, you can't comment out scripttags this way.

如果你真的被某些 HTML 片段所困扰——在一些无法控制的源代码中预渲染——其中包含评论,并且你需要确保没有任何内容在你的页面上呈现,你总是可以用一个script标签来包装它,如下所示,唯一的问题是,您不能以script这种方式注释掉标签。

<p>some text</p>

<!-- multiline "comment" below using script type="text/html" -->
<script type="text/html">

  <p>commented out html</p>

  <!-- comment 2

      // are nested html comment allowed?

    end of comment 2 -->

  <p>more commented out html</p>

</script>

<p>some more text</p>

If you need to comment out scripttags, you could use a textareaas wrapper instead, off course doing it this way, you can't comment out textareatags.

如果你需要注释掉script标签,你可以使用 atextarea作为包装器,当然这样做,你不能注释掉textarea标签。

<p>some text</p>

<!-- multiline "comment" below using textarea style="display:none;" -->
<textarea style="display:none;">

  <script>  

    alert("which won't show up..");  

  </script>

  <p>commented out html</p>

  <!-- comment 2

      // are nested html comment allowed?

    end of comment 2 -->

  <p>more commented out html</p>

</textarea>

<p>some more text</p>

回答by Dexter

Use templatetag. The fastest way to block all the comment and other html from showing up.

使用template标签。阻止所有评论和其他 html 显示的最快方法。

<template>
    <!-- first paragraph-->
    Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

    <!-- second paragraph-->
    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</template>

    <!-- third paragraph-->
    Ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.sunt in culpa qui officia deserunt mollit.

回答by Dirk Vollmar

Some editors have commenting/uncommenting commands which can automatically handle existing comments in a block of text. Visual Studio e.g. is doing that when you press Ctrl+KC and Ctrl+KU.

一些编辑器具有注释/取消注释命令,可以自动处理文本块中的现有注释。例如,当您按 Ctrl+KC 和 Ctrl+KU 时,Visual Studio 就是这样做的。

回答by TheHippo

I think it isn't allowed, but as far as I know it works in most of the major browsers except in Firefox.

我认为这是不允许的,但据我所知,它适用于除 Firefox 之外的大多数主要浏览器。

回答by drw

try using this

尝试使用这个

<!-- 

this is the start of the comment

这是评论的开始

<%-- this is another comment --%>

<%-- this is another one --%>

-->end of the comments.

-->评论结束。

回答by txavier

Try this

尝试这个

<p>some text</p>
<comment> comment 1
<p>commented out html</p>
<!-- comment 2
  // are nested html comment allowed?
end of comment 2 -->
<p>more commented out html</p>
end of comment 1 </comment>
<p>some more text</p>

回答by Paul Gorbas

A VS add-in that fakes nested comments by automatically converts <!--...-->to <!~~...~~>then comments out that whole section. It lets you toggle it on and off.

通过自动转换<!--...--><!~~...~~>然后注释掉整个部分来伪造嵌套注释的 VS 插件。它可以让您打开和关闭它。

nested-comments

嵌套注释