我需要在我的html文档中包含一些代码
我已尝试过<pre>
代码,但这没有帮助。
如何将此代码转换为文本文档?
由于
答案 0 :(得分:11)
答案 1 :(得分:8)
简答。
使用在线HTML Encoder对您的代码进行编码,然后将其放入pre
<pre>
<%--your encoded code goes here--%>
</pre>
长答案。
以上内容只会帮助您显示代码。如果您想为代码正确突出显示,请尝试 SyntaxHighlighter
答案 2 :(得分:4)
您可以使用<pre>
标记。每次在<pre>
标记内插入任何文本时,它都不会被解析为html文档。但有一点需要注意,如果你试图在pre标签中放置一个开头的HTML标签,你必须这样做:
<pre>
<TEST>
TEST!!
</TEST>
</pre>
答案 3 :(得分:0)
您可以结合使用<pre>
和<code>
标记。 <pre>
标记保留格式,<code>
标记以等宽字体输出。将<code>
标记包裹在<pre>
标记中,然后粘贴<code>
元素正文中的任何代码块。这将输出如下:
<pre>
<code>
function(){
var myVar = "This is code";
return myVar;
}
</code>
</pre>
答案 4 :(得分:0)
有些人可能会把我钉在十字架上而不是逃避我的代码。但这对我有用。
CSS
.tag:before{
content: '<'
}
.tag:after{
content: '>'
}
HTML
<pre>
<span class="tag">tag</span>
</pre>
<!--Instead of having to escaping all the character-->
<tag> </tag>
<!--Kinda interested to see why this is bad. I understand that not escaping code can be dangerous b/c of SQL injections and all sort of BS but why is this not a viable option-->
答案 5 :(得分:0)
使用 encode
示例:
<br> -> encoded -> <br>
use <br> in your text
与Ibu相同的答案,但也许您想要一种快速编码标签的方法。
答案 6 :(得分:0)
要完全不转义任何字符,可以使用文本区域:
textarea {
font-family: inherit;
font-size: inherit;
border: none;
background-color: transparent;
resize: none;
outline: none;
}
<div>In order to show a bullet point in html use the li tags:</div>
<div>
<textarea readonly><li>Hello</li></textarea>
</div>
<div>And this is what it will look like:</div>
<li>Hello</li>
运行该代码段,并注意
现在为什么我们需要CSS以及额外的HTML标签和属性?
CSS会删除textarea中的所有样式,因为textarea通常将包括边框样式,调整抓手大小并使用“输入”样式字体。 textarea标记需要使用“只读”属性,因此用户无法对其进行编辑。文本区域周围的多余div标签使文本区域可以更正确地插入文档流中。
它有一些额外的步骤,它会极大地改变DOM,但是如果您确实需要严格显示文本而又不因任何原因而完全不转义任何字符。
答案 7 :(得分:0)
使用<h1>This is a heading.</h1>
<p>This is a pharagraph</p>
<xmp>
<h1>This is a heading.</h1>
<p>This is a pharagraph</p>
</xmp>
标签。它比使用HTML编码器更容易,更快捷。示例:
int main() {
int f = open("file1", O_RDWR);
char buffer[4096];
while ( read(f, buffer, 4096) > 0 ) {
printf("%s", buffer);
}
}