我想实际显示 HTML 注释代码

I want to actually show the HTML comment code

我希望 window 中的输出显示实际的 "<!-- -->"。我如何引用它以便打印出来? 同样打印出 <h2>Text Here</h2> - 我希望它实际打印出来,而不是 <h2> 风格。

方法一:转义字符(推荐选项)


&lt; 是 <

&gt; 是 >

看看这段代码:

This will appear as a comment:
<br><!-- Test -->
<br>
<br>This will actually appear:
<br>&lt;!-- Test --&gt;

您可以在此处了解如何转义字符:https://ascii.cl/htmlcodes.htm


方法二:使用xmp标签


警告: MDN 网络文档不鼓励使用此功能:

This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

来源:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/xmp

This will appear as a comment:
<br><!-- Test -->
<br>
<br>This will actually appear:
<br><xmp><!-- Test --></xmp>