我需要在 html <pre> 标签内转义什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1273145/
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
What do I need to escape inside the html <pre> tag
提问by Alec Jacobson
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 吗?
采纳答案by Salman A
What happens if you use the <pre>
tag to display HTML markup on your blog:
如果您使用<pre>
标记在博客上显示 HTML 标记会发生什么:
<pre>Use a <span style="background: yellow;">span tag with style attribute</span> to hightlight words</pre>
This will pass HTML validation, but does it produce the expected result? No. The correct way is:
这将通过 HTML 验证,但它会产生预期的结果吗?没有。正确的方法是:
<pre>Use a <span style="background: yellow;">span tag with style attribute</span> to hightlight words</pre>
Another example: if you use the pre tag to display some other language code, the HTML encoding is still required:
再举一个例子:如果你使用 pre 标签来显示一些其他语言的代码,仍然需要 HTML 编码:
<pre>if (i && j) return;</pre>
This might produce the expected result but does it pass HTML validation? No. The correct way is:
这可能会产生预期的结果,但它是否通过了 HTML 验证?没有。正确的方法是:
<pre>if (i && j) return;</pre>
Long story short, HTML-encode the content of a pre tag just the way you do with other tags.
长话短说,HTML 编码 pre 标签的内容就像你对其他标签所做的一样。
回答by jave.web
The "Only For You" - HTML "fosile" version: using <xmp>
tag
“只为你” - HTML“fosile”版本:使用<xmp>
标签
This is not well known, but it really does exist and even chrome still supports it,
however using pair<xmp>
tag is NOT recommendedto be relied on - it's just for you, but its a very simple way how to do your personal e.g. DOCS. even w3.org WIKI says in example "No, really. don't use it."
这不是众所周知的,但它确实存在,甚至 chrome 仍然支持它,但是不建议依赖使用pair<xmp>
标签- 它只适合你,但它是一种非常简单的方法,如何做你的个人,例如 DOCS。甚至 w3.org WIKI 在示例中说“不,真的。不要使用它。”
You can put ANY html (excluding </xmp>
end tag) inside <xmp></xmp>
你可以把任何 html(不包括</xmp>
结束标签)放在里面<xmp></xmp>
<xmp>
<html> <br> just any other html tags...
</xmp>
The proper version
正确的版本
Proper version could be considered a HTML stored as STRINGand displayed with the help of some escaping function.
Just remember one thing - the strings in C-like languages are ususally written between single quotes or double quotes - if you wrap your string in double => you should escape doubles (problably with \
), if you wrap your string in single => escape singles (probably with \
)...
正确的版本可以被认为是存储为STRING的 HTML,并在一些转义函数的帮助下显示。
请记住一件事 - 类 C 语言中的字符串通常写在单引号或双引号之间 - 如果你用 double => 包裹你的字符串,你应该转义双打(可能用\
),如果你用单引号包裹你的字符串 => 转义单打(可能与\
)...
The most common way - Server-side language escaping (ex. in PHP)
最常见的方式 - 服务器端语言转义(例如在 PHP 中)
Server-side scripting languages often have some built-in function to escape HTML.
服务器端脚本语言通常有一些内置函数来转义 HTML。
<?php
$html = "<html> <br> or just any other HTML"; //store html
echo htmlspecialchars($html); //display escaped html
?>
The client-side way (example in JavaScript&jQuery)
客户端方式(JavaScript&jQuery 中的例子)
Similar approach as on server-side is achievable in client-side scripts,
JavaScript, from what I know, has no built-in function for that (it's quite logical), but if you use some framework/library, like jQuery - there are functions that can be used that way.
Just remember the same thing as for server-side - in C-like languages, escape the quotes you've wrapped your string in...
在客户端脚本中可以实现与服务器端类似的方法,据我所知,JavaScript 没有内置函数(这是非常合乎逻辑的),但是如果您使用一些框架/库,例如 jQuery - 有可以这样使用的功能。
请记住与服务器端相同的事情 - 在类 C 语言中,转义您将字符串包裹在其中的引号...
var html = '<html> <br> or just any other HTML';
var $elementToInsertEscapedHTMLto = jQuery("XXX"); //XXX is selector, e.g. CSS selector
$elementToInsertEscapedHTMLto.text( html );
回答by JMP
回答by PanicBus
Use this and don't worry about any of them.
使用它,不要担心它们中的任何一个。
<pre>
${fn:escapeXml('
<!-- all your code -->
')};
</pre>
You'll need to have jQuery enabled for it to work.
您需要启用 jQuery 才能使其工作。