我需要在html< pre>内转义什么?标签 [英] What do I need to escape inside the html <pre> tag
问题描述
我在博客中使用<pre>
标记来发布代码.我知道我必须将<
更改为<
,将>
更改为>
.我还需要转义其他字符以获取正确的html吗?
I use the <pre>
tag in my blog to post code. I know I have to change <
to <
and >
to >
. Are any other characters I need to escape for correct html?
推荐答案
如果使用<pre>
标记在博客上显示HTML标记会发生什么:
What happens if you use the <pre>
tag to display HTML markup on your blog:
<pre>Use a <span style="background: yellow;">span tag with style attribute</span> to hightlight words</pre>
这将通过HTML验证,但是会产生预期的结果吗? 否.正确的方法是:
This will pass HTML validation, but does it produce the expected result? No. The correct way is:
<pre>Use a <span style="background: yellow;">span tag with style attribute</span> to hightlight words</pre>
另一个示例:如果使用pre标签显示其他语言代码,则仍需要HTML编码:
Another example: if you use the pre tag to display some other language code, the HTML encoding is still required:
<pre>if (i && j) return;</pre>
这可能会产生预期的结果,但是它可以通过HTML验证吗? 否.正确的方法是:
This might produce the expected result but does it pass HTML validation? No. The correct way is:
<pre>if (i && j) return;</pre>
长话短说,就像使用其他标签一样,对pre标签的内容进行HTML编码.
Long story short, HTML-encode the content of a pre tag just the way you do with other tags.
这篇关于我需要在html< pre>内转义什么?标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!