Are nested HTML comments possible?

2019-01-07 08:39发布

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

<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?

9条回答
相关推荐>>
2楼-- · 2019-01-07 08:50

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

查看更多
对你真心纯属浪费
3楼-- · 2019-01-07 08:54

try using this

<!-- 

this is the start of the comment

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

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

--> end of the comments.

查看更多
看我几分像从前
4楼-- · 2019-01-07 08:59

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 script tag like below, only thing is, you can't comment out script tags this way.

<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 script tags, you could use a textarea as wrapper instead, off course doing it this way, you can't comment out textarea tags.

<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>

查看更多
倾城 Initia
5楼-- · 2019-01-07 09:01

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

查看更多
Luminary・发光体
6楼-- · 2019-01-07 09:07

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>
查看更多
地球回转人心会变
7楼-- · 2019-01-07 09:08

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.

查看更多
登录 后发表回答