How to quote in HTML (not blockquote)?

2020-04-07 04:55发布

I want to some sample code in a html page. How do I do it, for example I want to show a tag and image tag, in stackoverflow I use 4 spaces:

A Tag example:
<a href="your_url"></a>

Image Tag example:
<img...>

标签: html quotes
3条回答
\"骚年 ilove
2楼-- · 2020-04-07 05:20

You can use a combination of the <pre> and <code> tags. The <pre> tag formats everything inside of it as is appears in the source code (useful for showing large blocks of source), and the <code> tag is used for inline formatting of code (similar to how the tags in this answer are formatted). Also, anything inside the <code> tags need to be converted into HTML special characters (& gt; and & lt; for example).

查看更多
够拽才男人
3楼-- · 2020-04-07 05:21

To format the output, you can use the pre element.

Howver, you will still need to turn the special characters < > ' " & into their HTML entities &lt; &gt; and so on so the HTML tags turn up in clear text and don't get rendered by the browser.

If you can use PHP, there is htmlspecialchars() for that. Every server-side scripting language has an equivalent of it.

In JavaScript, setting the innerText property of the pre container leads to the characters being displayed literally and not as interpreted HTML.

If you want to do it manually, this is what you need to convert:

& to &amp;
> to &gt;
< to &lt;
" to &quot;
' to &#039

source

查看更多
相关推荐>>
4楼-- · 2020-04-07 05:27

I think you're asking for the <pre> tag for preformatted text.

Everything that goes inside the pre tag it's not interpreted.

Try this on a browser:

<pre>
&lt;img src="whatever" /&gt;
&lt;othertag&gt;whatever&lt;/otherta&gt;
</pre>

EDIT: As Pekka pointed out, for HTML tags, symbols must be converted to their HTML entities.

查看更多
登录 后发表回答