在 HTML 中显示 HTML 片段 [英] Display HTML snippets in HTML

查看:37
本文介绍了在 HTML 中显示 HTML 片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在网页上显示 HTML 片段而不需要将每个 < 替换为 &lt; 并将 > 替换为 <代码>>?

How can I show HTML snippets on a webpage without needing to replace each < with &lt; and > with &gt;?

换句话说,是否有一个标签在你点击结束标签之前不要渲染 HTML?

In other words, is there a tag for don't render HTML until you hit the closing tag?

推荐答案

是否有在点击结束标记之前不呈现 HTML 的标记?

,没有.在 HTML 中,转义一些字符是必不可少的:

No, there is not. In HTML proper, there’s no way short of escaping some characters:

  • &&amp;
  • <&lt;
  • & as &amp;
  • < as &lt;

(顺便说一句,没有必要逃避>,但人们经常出于对称的原因这样做.)

(Incidentally, there is no need to escape > but people often do it for reasons of symmetry.)

当然,您应该将生成的转义 HTML 代码括在 <pre><code>...</code></pre> 中以 (a) 保留空格和行中断,并且 (b) 将其标记为代码元素.

And of course you should surround the resulting, escaped HTML code within <pre><code>…</code></pre> to (a) preserve whitespace and line breaks, and (b) mark it up as a code element.

所有其他解决方案,例如将您的代码包装到