我需要在html< pre>内转义什么?标签 [英] What do I need to escape inside the html <pre> tag

查看:195
本文介绍了我需要在html< pre>内转义什么?标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在博客中使用<pre>标记来发布代码.我知道我必须将<更改为&lt;,将>更改为&gt;.我还需要转义其他字符以获取正确的html吗?

I use the <pre> tag in my blog to post code. I know I have to change < to &lt; and > to &gt;. 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 &lt;span style=&quot;background: yellow;&quot;&gt;span tag with style attribute&lt;/span&gt; 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 &amp;&amp; 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&lt; pre&gt;内转义什么?标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆