如何在网页中显示文字 HTML 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6763148/
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 show literal HTML script in a web page?
提问by pmerino
I have some HTML script that I would like to make it display in a web page like the code option here:
我有一些 HTML 脚本,我想让它显示在网页中,例如此处的代码选项:
<a href="#">test</a>
How can I do that?
我怎样才能做到这一点?
回答by SLaks
You need to HTML-escape your text, as you can easily tell if you look at the HTML source of this very page:
您需要对文本进行 HTML 转义,因为如果您查看此页面的 HTML 源代码,就可以轻松判断:
<div class="post-text">
<p>I have some HTML that I would like to make it appear like the code option here</p>
<pre><code><a href="#">test</a>
</code></pre>
<p>How can I do it?</p>
</div>
回答by Jamie Dixon
You'll need to encode the angle brackets etc in order to show this on the page.
您需要对尖括号等进行编码才能在页面上显示。
You can do this manually in html as:
您可以在 html 中手动执行此操作:
<a href="#">test</a>
Where:
在哪里:
< = <
> = >
For other characters here's a table of character codes: http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
对于其他字符,这里有一个字符代码表:http: //en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references