【问题标题】:HTML comments within comments?评论中的 HTML 评论?
【发布时间】:2011-04-15 23:32:55
【问题描述】:

有没有办法评论多行...其中已经有一些 cmets?

<html>
<!-- Multi-line comment begin
  <head>
    <!-- This script does abcxyz -->
    <script>...</script>
  </head>
  <body>
    Hello world!
  </body>
Multi-line comment end -->
</html>

似乎连 SO 的语法高亮都不接受这个……

【问题讨论】:

  • 这有点烦人,但是您可以通过删除每个 cmets 的 --&gt; 部分来保留其他 cmets。

标签: html multiline comments


【解决方案1】:

我认为关键是这样的:

注意 cmets 是标记。

http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.4

这不是有效的标记:

<div <span/> />

...所以你提到的也不是。


因为我所有的网站都是用 PHP 编写的,所以我通常用 PHP cmets 注释掉代码:

<?/*?>
<div>...</div>
<p>...</p>
<?*/?>

也许你可以使用类似的技巧。

【讨论】:

  • 嘘,真的吗?我是唯一一个因此而恶化的人吗? o.O 感谢您的 php 建议...
【解决方案2】:

没有。注释不能嵌套,HTML 只有一种注释样式。

【讨论】:

    【解决方案3】:

    没有。结束评论标签 --> 将始终结束评论部分,因此如果您的评论包含评论,则包含的评论的结束标签将结束评论部分。

    如果您选择撤消评论,您可以在即将注释掉的部分中替换 -->

    【讨论】:

    • 大声笑。我想这样做是可行的......:&lt;--blahblah&lt;--moreblahblah--&lt;blahbleebloblah--&gt;(然后当你取消注释时,你会得到:blahblah&lt;--moreblahblah--&lt;blahbleebloblah,这至少是一大堆,所以很明显 - 然后你只需将 )。
    【解决方案4】:

    如果你真的被一些 HTML 卡住了——在一些不可控的源预渲染——它包含 cmets,并且你需要确保它没有在你的页面上呈现,你总是可以用 @ 包裹它987654321@标签如下,唯一的问题是,你不能这样注释掉script标签。

     <html>
       <head>
       </head>
       <body>
         <!-- multiline "comment" below using script type="text/html" -->
         <script type="text/html">        
            Hello world!
            <!-- Look at me, I'm a comment :) -->
            <div>Yeah, whatever, I'm an element..</div>        
        </script>
        <span>Who cares, span is the man, the only visible one anyway!</span>
      </body>
    </html>

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

    <html>
      <head>
      </head>
      <body>
        <!-- multiline "comment" below using textarea style="display:none;" -->
        <textarea style="display:none;">	
          <script>  
            alert("which won't show up..");  
          </script>
          Hello world!
          <!-- Look at me, I'm a comment :) -->
          <div>Yeah, whatever, I'm an element..</div>        
        </textarea>
        <span>Who cares, span is the man, the only visible one anyway!</span>
      </body>
    </html>

    【讨论】:

      【解决方案5】:

      不,不幸的是 HTML cmets 不嵌套。

      【讨论】:

        【解决方案6】:

        它可能对某些开发人员仍然有用,如果使用 vsCode 作为您的 IDE,您可以在 Visual Studio 代码市场中使用一个名为 Nest Comments 的扩展,它的工作原理就像一个魅力。

        这是链接 Nest Comments

        【讨论】:

        • 这对我很有用。感谢分享!
        【解决方案7】:

        可以将其嵌入单引号或双引号中作为标签属性。这当然要求相应的引号不会出现在两者之间。

        <html>
        <br comm='Multi-line comment begin
          <head>
            <!-- This script does abcxyz -->
            <script>...</script>
          </head>
          <body>
            Hello world!
          </body>
        Multi-line comment end'>
        </html>
        

        http://jsfiddle.net/cehfumb2/2/

        【讨论】:

          猜你喜欢
          • 2014-07-06
          • 2021-09-09
          • 1970-01-01
          • 1970-01-01
          • 2011-12-18
          • 2013-07-10
          • 2014-12-11
          • 1970-01-01
          • 2016-03-21
          相关资源
          最近更新 更多