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

换句话说,是否有一个标记表示在点击结束标记之前不呈现HTML ?


当前回答

我认为:

你想要编写100%有效的HTML5 你想把代码片段(几乎)文字放在HTML中 特别是<应该不需要转义

你所有的选项都在这棵树里:

with HTML syntax there are five kinds of elements those called "normal elements" (like <p>) can't have a literal < it would be considered the start of the next tag or comment void elements they have no content you could put your HTML in a data attribute (but this is true for all elements) that would need JavaScript to move the data elsewhere in double-quoted attributes, " and &thing; need escaping: &quot; and &amp;thing; respectively raw text elements <script> and <style> only they are never rendered visible but embedding your text in Javascript might be feasable Javascript allows for multi-line strings with backticks it could then be inserted dynamically a literal </script is not allowed anywhere in <script> escapable raw text elements <textarea> and <title> only <textarea> is a good candidate to wrap code in it is totally legal to write </html> in there not legal is the substring </textarea for obvious reasons escape this special case with &lt;/textarea or similar &thing; needs escaping: &amp;thing; foreign elements elements from MathML and SVG namespaces at least SVG allows embedding of HTML again... and CDATA is allowed there, so it seems to have potential with XML syntax covered by Konrad's answer

 

注意:>从来不需要转义。甚至在正常元素中也没有。

其他回答

是否有一个标签表示在点击结束标签之前不呈现HTML ?

不,没有。在HTML中,除了转义某些字符外,没有其他方法:

& as & < as &lt;

(顺便说一句,没有必要转义>,但人们经常这样做是为了对称。)

当然,您应该将结果转义的HTML代码包围在<pre><code>…</code></pre>中,以(a)保留空格和换行符,并(b)将其标记为代码元素。

所有其他解决方案,如将代码包装到<textarea>或(已弃用)<xmp>元素,都会破坏1

向浏览器声明为XML的XHTML(通过HTTP Content-Type报头!-仅仅设置DOCTYPE是不够的)也可以使用CDATA部分:

<![CDATA[Your <code> here]]>

但这只适用于XML,而不适用于HTML,而且这也不是一个万无一失的解决方案,因为代码不能包含结束分隔符]]>。因此,即使在XML中,最简单、最健壮的解决方案也是通过转义。


1 .恰当的例子:

不;教室:100%;} < textarea readonly =“readonly > <p>电脑<textarea>说</textarea> <span>no.</span> < / textarea > < xmp > 计算机<xmp>说</xmp> <span>no.</span> < / xmp >

你可以使用像PHP这样的服务器端语言来插入原始文本:

<?php
  $str = <<<EOD
  <html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="description" content="Minimal HTML5">
    <meta name="keywords" content="HTML5,Minimal">
    <title>This is the title</title>
    <link rel='stylesheet.css' href='style.css'>
  </head>
  <body>
  </body>
  </html>
  EOD;
?>

然后转储$str htmlencoded的值:

<div style="white-space: pre">
  <?php echo htmlentities($str); ?>
</div>

这是一个简单的技巧,我已经在Safari和Firefox中尝试过了

<code>
    <span><</span>meta property="og:title" content="A very fine cuisine" /><br>
    <span><</span>meta property="og:image" content="http://www.example.com/image.png" />
</code>

它将显示如下:

你可以在这里现场观看

实际上有一种方法可以做到。它有一个限制(1),但是是100%标准的,没有被弃用(像xmp一样),并且可以工作。

这是微不足道的。下面就是:

<div id="mydoc-src" style="display: none;">
LlNnlljn77fggggkk77csJJK8bbJBKJBkjjjjbbbJJLJLLJo
<!--
YOUR CODE HERE.
    <script src="WidgetsLib/all.js"></script>
    ^^ This is a text, no side effects trying to load it.
-->
LlNnlljn77fggggkk77csJJK8bbJBKJBkjjjjbbbJJLJLLJo
</div>

请听我解释。首先,普通的HTML注释可以防止整个块被解释。你可以很容易地在其中添加任何标签,它们都将被忽略。从解释中忽略,但仍然可以通过innerHTML!因此,剩下的就是获取内容,并过滤前面和后面的注释标记。

除了(记住-限制)你不能把里面的HTML注释,因为(至少在我的Chrome)他们的嵌套是不支持的,和非常第一个'——>'将结束显示。

好吧,这是一个讨厌的小限制,但在某些情况下,如果你的文本没有HTML注释,这根本不是问题。而且,逃避一个构念比逃避一堆构念容易。

Now, what is that weird LlNnlljn77fggggkk77csJJK8bbJBKJBkjjjjbbbJJLJLLJo string? It's a random string, like a hash, unlikely to be used in the block, and used for? Here's the context, why I have used it. In my case, I took the contents of one DIV, then processed it with Showdown markdown, and then the output assigned into another div. The idea was, to write markdown inline in the HTML file, and just open in a browser and it would transform on the load on-the-fly. So, in my case, <!-- became transformed to <p><!--</p>, the comment properly escaped. It worked, but polluted the screen. So, to easily remove it with regex, the random string was used. Here's the code:

    var converter = new showdown.Converter();
    converter.setOption('simplifiedAutoLink', true);
    converter.setOption('tables', true);
    converter.setOption('tasklists', true);
    var src = document.getElementById("mydoc-src");
    var res = document.getElementById("mydoc-res");
    res.innerHTML = converter.makeHtml(src.innerHTML)
            .replace(/<p>.{0,10}LlNnlljn77fggggkk77csJJK8bbJBKJBkjjjjbbbJJLJLLJo.{0,10}<\/p>/g, "");
    src.innerHTML = '';

这很有效。

如果有人感兴趣,本文就是使用这种技术编写的。请随意下载,并查看HTML文件。

这取决于你用它来做什么。是用户输入吗?然后使用<textarea>,并转义所有内容。在我的例子中(可能也是您的例子),我只是使用了注释,它就完成了工作。

如果你不使用markdown,只是想从标签中得到它,那么它甚至更简单:

<div id="mydoc-src" style="display: none;">
<!--
YOUR CODE HERE.
    <script src="WidgetsLib/all.js"></script>
    ^^ This is a text, no side effects trying to load it.
-->
</div>

和JavaScript代码来获取它:

    var src = document.getElementById("mydoc-src");
    var YOUR_CODE = src.innerHTML.replace(/(<!--|-->)/g, "");

在HTML ?不。

在XML / XHTML吗?您可以使用CDATA块。