Html 如何让html忽略作为文本一部分的代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6632873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make html ignore code that is part of text?
提问by Tarek Saied
I need to include some codes in my html document
我需要在我的 html 文档中包含一些代码
I've tried <pre>
tag, but that didn't help.
我试过<pre>
标记,但这没有帮助。
How do I get this code into a document like text?
如何将此代码放入文本等文档中?
Thanks
谢谢
回答by naveen
Short Answer.
简答。
Encode your code using an online HTML Encoderand then put it inside pre
使用在线HTML 编码器对您的代码进行编码,然后将其放入pre
<pre>
<%--your encoded code goes here--%>
</pre>
Long Answer.
长答案。
The above will only help you to show your code. If you want proper highlighting for your code, Try something like SyntaxHighlighter
以上只会帮助您显示您的代码。如果您想要正确突出显示您的代码,请尝试使用SyntaxHighlighter 之类的东西
回答by Ibu
You have to use html entities. Example:
您必须使用html 实体。例子:
<div>
Some Stuff
</div>
should be
应该
<div>
Some Stuff
</div>
and it will render as the first one
它将呈现为第一个
回答by Benny Tjia
You can use <pre>
tag. Each time you insert any texts within the <pre>
tag it wont get parsed as html document. One caveat though, if you try to put an opening HTML tag inside the pre tag, you have to do it like this:
您可以使用<pre>
标签。每次在<pre>
标签中插入任何文本时,它都不会被解析为 html 文档。但需要注意的是,如果您尝试在 pre 标签中放置一个 HTML 开始标签,您必须这样做:
<pre>
<TEST>
TEST!!
</TEST>
</pre>
回答by Michel Hébert
You can use a combination of the <pre>
and <code>
tags. The <pre>
tag retains the formatting , and the <code>
tag outputs in monospaced font. Wrap the <code>
tag in <pre>
tag, and paste whatever block of code in the <code>
elements body. This will output like the following:
您可以使用<pre>
和<code>
标签的组合。的<pre>
标签保留的格式,并且<code>
在宽字体标签输出。将<code>
标签包裹在标签中<pre>
,并将任何代码块粘贴到<code>
元素主体中。这将输出如下:
<pre>
<code>
function(){
var myVar = "This is code";
return myVar;
}
</code>
</pre>
<pre>
<code>
function(){
var myVar = "This is code";
return myVar;
}
</code>
</pre>
回答by Harshmellow
Some people might crucify me not escaping my code. But this worked for me.
有些人可能会把我钉在十字架上,而不是逃避我的代码。但这对我有用。
CSS
CSS
.tag:before{
content: '<'
}
.tag:after{
content: '>'
}
HTML
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-->