如何在网页上显示HTML片段,而不需要将每个<替换为<>和>?
换句话说,是否有一个标记表示在点击结束标记之前不呈现HTML ?
如何在网页上显示HTML片段,而不需要将每个<替换为<>和>?
换句话说,是否有一个标记表示在点击结束标记之前不呈现HTML ?
当前回答
<code><?php $str='<';echo htmlentities($str);?></code>
我发现这是最简单、最快和最紧凑的方法。
其他回答
示例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>
这是目前为止在大多数情况下最好的方法:
<pre><code>
code here, escape it yourself.
</code></pre>
我会投票给第一个提出这个建议的人,但我没有名气。为了那些试图在网上找到答案的人,我不得不说些什么。
我使用<xmp>,就像这样: http://jsfiddle.net/barnameha/hF985/1/
这是一种hack,但是我们可以使用类似的东西:
正文脚本{ 显示:块; 字体类型:等宽字体; 空白:前; } <脚本type = " text / html " > <标题> Hello World h1 > < / < ul > <li>享受这个狡猾的黑客, <李>或不! < / ul > > < /脚本
使用该CSS,浏览器将在主体中显示脚本。它不会尝试执行这个脚本,因为它有一个未知的类型text/html。在<script>中没有必要转义特殊字符,除非你想包含一个结束</script>标记。
我正在使用类似这样的东西在页面主体中显示可执行的JavaScript,这是一种“有文化的编程”。
在这个问题中还有更多的信息,标签什么时候应该可见,为什么可以?
你可以使用像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>