代码之家  ›  专栏  ›  技术社区  ›  QAZ

是否可以嵌套HTML注释?

  •  82
  • QAZ  · 技术社区  · 16 年前

    根据标题;是否可以在有效的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>
    

    似乎没有,有人知道我怎样才能让嵌套的注释工作吗?

    9 回复  |  直到 6 年前
        1
  •  84
  •   johny why    6 年前

    嵌套注释时,请将“--”替换为“--”。当你卸除巢穴时,颠倒程序。这不是 <!-- 这是禁止的,但是 -- .

    例子:

    <!-- some stuff
    <!- - some inner stuff - ->
    <!- - a sibling - ->
    the footer -->
    
        2
  •  89
  •   Dave Land    9 年前

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

    简短回答:

    HTML注释并不是很多人认为的那样。HTML是SGML的一种形式,其中注释由成对的双破折号分隔。( —— 艾斯 —— )

    因此,在一对尖括号内的任何一对双破折号,在左括号后有一个感叹号。( <! —— 艾斯 —— > )是注释。规格说明比我能说的更好: http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.4

    这就是为什么像这样的评论 全部的 可能一次或另一次)是 坏的 想法:

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

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

    我不那么懒惰了:这个所谓的“评论”实际上包括10条评论, 任何评论之外的三个词(即,只是坏的sgml),以及 未终止的注释的开头。真是一团糟:

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

    当然不是 相当地 这很简单,因为每个浏览器选择解释规范的方式不同。

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

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

    答案很长:为什么我们会出错

    我们中的大多数人都是在HTML的基础上长大的(没有深入研究它下面的SGML)。 <!—— 开始注释,字符串 ---GT; 结束评论。

    事实上, <! > 在HTML文档中分隔一个SGML声明,例如我们在页面顶部看到的doctype声明。 一个SGML声明,注释由双破折号分隔。因此,HTML注释

    <!-- this is a comment -->

    我们大多数人都相信是这样分析的 <!—— 这是一条评论 ---GT; 实际分析如下:
    <! —— 这是一条评论 —— > . 它是一个除了注释之外为空的SGML声明。

    因为HTML是SGML的一种形式,所以这个“comment-within-a-declaration”用作HTML注释。

    出于兴趣,这里有一块纯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 --
    >
        3
  •  7
  •   Pang Ajmal PraveeN    8 年前

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

        4
  •  5
  •   Dexter    6 年前

    使用 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.
    
        5
  •  4
  •   Mathijs Flietstra    7 年前

    如果你真的被一些包含评论的不可控制的源代码预先渲染的HTML“卡住了,你需要确保没有任何一个被渲染到你的页面上,你可以用一个 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>

    如果你需要评论的话 脚本 标签,您可以使用 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>
        6
  •  1
  •   Dirk Vollmar    16 年前

    有些编辑器具有注释/取消注释命令,这些命令可以自动处理文本块中的现有注释。例如,当您按下ctrl+kc和ctrl+ku时,Visual Studio正在执行此操作。

        7
  •  0
  •   TheHippo    16 年前

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

        8
  •  0
  •   txavier    8 年前

    试试这个

    <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>
    
        9
  •  -1
  •   Rathakrishnan Ramasamy micky    12 年前

    尝试使用这个

    <!-- 
    

    这是评论的开头

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

    --> 评论结束。