如何在网页中显示文字 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:39:20  来源:igfitidea点击:

How to show literal HTML script in a web page?

html

提问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>&lt;a href="#"&gt;test&lt;/a&gt;
    </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 中手动执行此操作:

&lt;a href="#"&gt;test&lt;/a&gt;

Where:

在哪里:

&lt; = <

&gt; = >

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