如何使html忽略作为文本一部分的代码?

时间:2022-11-29 12:53:16

I need to include some codes in my html document

我需要在我的html文档中包含一些代码

I've tried <pre> tag, but that didn't help.

我试过

标签,但这没有帮助。

How do I get this code into a document like text?

如何将此代码转换为文本文档?

Thanks

谢谢

6 个解决方案

#1


7  

Short Answer.

简答。

Encode your code using an online HTML Encoder and then put it inside pre

使用在线HTML编码器对代码进行编码,然后将其放入预先设置

<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

Link: How to use SyntaxHighlighter.

链接:如何使用SyntaxHighlighter。

#2


10  

You have to use html entities. Example:

你必须使用html实体。例:

<div>
   Some Stuff
</div>

should be

应该

&lt;div&gt;
   Some Stuff
&lt;/div&gt;

and it will render as the first one

它将呈现为第一个

#3


4  

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:

您可以使用

标签。每次在
标记内插入任何文本时,它都不会被解析为html文档。但有一点需要注意,如果你试图在pre标签中放置一个开头的HTML标签,你必须这样做:

<pre>
     &lt;TEST>
            TEST!!
     &lt;/TEST>
</pre>

#4


0  

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> function(){ var myVar = "This is code"; return myVar; } </code> </pre>

  function(){var myVar =“这是代码”;返回myVar; } 
  
   
  
  

#5


0  

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-->
&lt;tag&gt; &lt;&#47;tag&gt;

<!--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-->

#6


0  

Use encode

使用编码

Example:

例:

<br> -> encoded -> &lt;br&gt;

use &lt;br&gt; in your text

same answer as Ibu but maybe you want a fast way to encode your tags.

与Ibu相同的答案,但也许您想要一种快速编码标签的方法。

#1


7  

Short Answer.

简答。

Encode your code using an online HTML Encoder and then put it inside pre

使用在线HTML编码器对代码进行编码,然后将其放入预先设置

<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

Link: How to use SyntaxHighlighter.

链接:如何使用SyntaxHighlighter。

#2


10  

You have to use html entities. Example:

你必须使用html实体。例:

<div>
   Some Stuff
</div>

should be

应该

&lt;div&gt;
   Some Stuff
&lt;/div&gt;

and it will render as the first one

它将呈现为第一个

#3


4  

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:

您可以使用

标签。每次在
标记内插入任何文本时,它都不会被解析为html文档。但有一点需要注意,如果你试图在pre标签中放置一个开头的HTML标签,你必须这样做:

<pre>
     &lt;TEST>
            TEST!!
     &lt;/TEST>
</pre>

#4


0  

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> function(){ var myVar = "This is code"; return myVar; } </code> </pre>

  function(){var myVar =“这是代码”;返回myVar; } 
  
   
  
  

#5


0  

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-->
&lt;tag&gt; &lt;&#47;tag&gt;

<!--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-->

#6


0  

Use encode

使用编码

Example:

例:

<br> -> encoded -> &lt;br&gt;

use &lt;br&gt; in your text

same answer as Ibu but maybe you want a fast way to encode your tags.

与Ibu相同的答案,但也许您想要一种快速编码标签的方法。