如何在网页上显示HTML片段,而不需要将每个<替换为<>和>?
换句话说,是否有一个标记表示在点击结束标记之前不呈现HTML ?
如何在网页上显示HTML片段,而不需要将每个<替换为<>和>?
换句话说,是否有一个标记表示在点击结束标记之前不呈现HTML ?
当前回答
示例1:
<pre>
This text has
been formatted using
the HTML pre tag. The brower should
display all white space
as it was entered.
</pre>
示例2:
<pre>
<code>
My pre-formatted code
here.
</code>
</pre>
示例3: (如果你实际上是在“引用”一段代码,那么标记应该是)
<blockquote>
<pre>
<code>
My pre-formatted "quoted" code here.
</code>
</pre>
</blockquote>
其他回答
这是一种hack,但是我们可以使用类似的东西:
正文脚本{ 显示:块; 字体类型:等宽字体; 空白:前; } <脚本type = " text / html " > <标题> Hello World h1 > < / < ul > <li>享受这个狡猾的黑客, <李>或不! < / ul > > < /脚本
使用该CSS,浏览器将在主体中显示脚本。它不会尝试执行这个脚本,因为它有一个未知的类型text/html。在<script>中没有必要转义特殊字符,除非你想包含一个结束</script>标记。
我正在使用类似这样的东西在页面主体中显示可执行的JavaScript,这是一种“有文化的编程”。
在这个问题中还有更多的信息,标签什么时候应该可见,为什么可以?
//To show xml tags in table columns you will have to encode the tags first
function htmlEncode(value) {
//create a in-memory div, set it's inner text(which jQuery automatically encodes)
//then grab the encoded contents back out. The div never exists on the page.
return $('<div/>').text(value).html();
}
html = htmlEncode(html)
已弃用,但适用于FF3和IE8。
<xmp>
<b>bold</b><ul><li>list item</li></ul>
</xmp>
推荐:
<pre><code>
code here, escape it yourself.
</code></pre>
最终,最好的答案(尽管很烦人)是“转义文本”。
然而,有很多文本编辑器——甚至是独立的迷你实用程序——可以自动完成这项工作。所以如果你不想,你永远都不应该手动转义它(除非它是转义和未转义代码的混合…)
快速搜索谷歌会显示这个,例如:http://malektips.com/zzee-text-utility-html-escape-regular-expression.html
这是目前为止在大多数情况下最好的方法:
<pre><code>
code here, escape it yourself.
</code></pre>
我会投票给第一个提出这个建议的人,但我没有名气。为了那些试图在网上找到答案的人,我不得不说些什么。