Html 如何在 <pre> 标签内转义 < 和 >
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42182/
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 escape < and > inside <pre> tags
提问by urini
I'm trying to write a blog post which includes a code segment inside a <pre>
tag. The code segment includes a generic type and uses <>
to define that type. This is what the segment looks like:
我正在尝试写一篇博客文章,其中包含一个<pre>
标签内的代码段。代码段包括一个泛型类型并用于<>
定义该类型。这是该段的样子:
<pre>
PrimeCalc calc = new PrimeCalc();
Func<int, int> del = calc.GetNextPrime;
</pre>
The resulting HTML removes the <>
and ends up like this:
生成的 HTML 删除<>
和 最终是这样的:
PrimeCalc calc = new PrimeCalc();
Func del = calc.GetNextPrime;
How do I escape the <>
so they show up in the HTML?
我如何转义,<>
以便它们显示在 HTML 中?
回答by John Sheehan
<pre>
PrimeCalc calc = new PrimeCalc();
Func<int, int> del = calc.GetNextPrime;
</pre>
回答by Chris Marasti-Georg
<pre>></pre>
renders as:
呈现为:
>
So you want:
所以你要:
<pre>
PrimeCalc calc = new PrimeCalc();
Func<int, int> del = calc.GetNextPrime;
</pre>
which turns out like:
结果如下:
PrimeCalc calc = new PrimeCalc(); Func<int, int> del = calc.GetNextPrime;
回答by crashmstr
Use <
and >
to do <
and >
inside html.
使用<
and>
来做<
and>
里面的 html。
回答by ckpwong
<
and >
respectively
<
和>
分别
回答by toolkit
How about:
怎么样:
< and >
Hope this helps?
希望这可以帮助?
回答by akdom
What rp said, just replace the greater-than(>) and less-than(<) symbols with their html entity equivalent. Here's an example:
什么rp 说,只需用它们的 html 实体等效替换大于(>)和小于(<)符号。下面是一个例子:
<pre>
PrimeCalc calc = new PrimeCalc();
Func<int, int> del = calc.GetNextPrime;
</pre>
This should appear as (this time using exactly the same without the prepended spaces for markdown):
这应该显示为(这次使用完全相同,没有预先标记的空格):
PrimeCalc calc = new PrimeCalc(); Func<int, int> del = calc.GetNextPrime;
回答by OwenP
It's probably something specific to your blog software, but you might want to give the following strings a try (remove the underscore character): &_lt; &_gt;
这可能是特定于您的博客软件的内容,但您可能想尝试以下字符串(删除下划线字符): &_lt; &_gt;
回答by PanicBus
A better way to do is not to have to worry about the character codes at all. Just wrap all your code inside the <pre>
tags with the following
更好的方法是根本不必担心字符代码。只需<pre>
使用以下代码将所有代码包装在标签中
<pre>
${fn:escapeXml('
<!-- all your code -->
')};
</pre>
You'll need to have jQuery enabled for it to work, tho.
您需要启用 jQuery 才能使其工作,不过。