为什么代码在代码块中呈现?

时间:2022-09-10 18:27:58

I want to display HTML in a webpage. I've wrapped in a code block but the last two lines still execute/render. What am I doing wrong?

我想在网页中显示HTML。我已经包装在一个代码块中,但最后两行仍然执行/渲染。我究竟做错了什么?

<pre><code>
div {background: brown;}
div.bluebg {background: blue;}
<div>default brown background</div>
<div class="base">blue background</div>
</code></pre>

The last two lines were wrapped in div tags. I notice * strips them out. I don't want to strip them but modify I guess with &lt; and &gt;. Is there a listing of tags that should be modified to render them in a webpage? Is there an online program that can convert these to the above syntax?

最后两行包含在div标签中。我注意到*剥离它们。我不想剥离它们但是我想用<和>。是否有应该修改的标签列表以在网页中呈现它们?是否有可以将这些转换为上述语法的在线程序?

3 个解决方案

#1


I do not think [those tags] mean what you think they mean.

我不认为[那些标签]意味着你认为他们的意思。

<pre> allows you to preserve white space and line feeds. <code> allows you to semantically indicate that code is being displayed on your page. Both have some default styles (such as applying a fixed-width font), but neither one does anything to escape <, >, &, or ", so any unescaped HTML code you put in between those tags is going to be processed as HTML. You'll have to use &lt;, &gt;, &amp;, and &quot;. Here's a page where you can paste in text and have it escaped: http://accessify.com/tools-and-wizards/developer-tools/quick-escape/

允许您保留空白区域和换行符。 允许您在语义上指示代码正在页面上显示。两者都有一些默认样式(例如应用固定宽度的字体),但是没有人做任何事情来逃避<,>,&,或“,因此你在这些标签之间放置的任何非转义HTML代码将被处理为HTML你必须使用< ;,>,&和“。”这是一个页面,你可以粘贴文本并将其转义:http://accessify.com/tools-and-wizards/developer-工具/快速逃生/

#2


You need to replace < with &lt;, > with &gt; and & with &amp; and that's it.

您需要将 替换为>和&&就是这样。 <,>

#3


Judging by the contents of your post I assume you're talking about showing code in Markdown on Stack Overflow.

根据你帖子的内容判断我假设你正在谈论在Stack Overflow上的Markdown中显示代码。

Don't wrap your code in <pre> and <code>, just indent it by 4 spaces, like so:

不要将代码包装在

中,只需将其缩进4个空格,如下所示:

div {background: brown;}
div.bluebg {background: blue;}
<div>default brown background</div>
<div class="base">blue background</div>

Markdown actually parses HTML (at, least these tags) as you can see. Outside of markdown you'd still have to change <div> to &lt;div&gt; for it to render properly anyway - using code/pree doesn't stop the code from rendering.

正如您所见,Markdown实际上解析HTML(至少是这些标签)。在降价之外,您仍然需要将

更改为< div>无论如何都要正确呈现 - 使用code / pree不会阻止代码呈现。

#1


I do not think [those tags] mean what you think they mean.

我不认为[那些标签]意味着你认为他们的意思。

<pre> allows you to preserve white space and line feeds. <code> allows you to semantically indicate that code is being displayed on your page. Both have some default styles (such as applying a fixed-width font), but neither one does anything to escape <, >, &, or ", so any unescaped HTML code you put in between those tags is going to be processed as HTML. You'll have to use &lt;, &gt;, &amp;, and &quot;. Here's a page where you can paste in text and have it escaped: http://accessify.com/tools-and-wizards/developer-tools/quick-escape/

允许您保留空白区域和换行符。 允许您在语义上指示代码正在页面上显示。两者都有一些默认样式(例如应用固定宽度的字体),但是没有人做任何事情来逃避<,>,&,或“,因此你在这些标签之间放置的任何非转义HTML代码将被处理为HTML你必须使用< ;,>,&和“。”这是一个页面,你可以粘贴文本并将其转义:http://accessify.com/tools-and-wizards/developer-工具/快速逃生/

#2


You need to replace < with &lt;, > with &gt; and & with &amp; and that's it.

您需要将 替换为>和&&就是这样。 <,>

#3


Judging by the contents of your post I assume you're talking about showing code in Markdown on Stack Overflow.

根据你帖子的内容判断我假设你正在谈论在Stack Overflow上的Markdown中显示代码。

Don't wrap your code in <pre> and <code>, just indent it by 4 spaces, like so:

不要将代码包装在

中,只需将其缩进4个空格,如下所示:

div {background: brown;}
div.bluebg {background: blue;}
<div>default brown background</div>
<div class="base">blue background</div>

Markdown actually parses HTML (at, least these tags) as you can see. Outside of markdown you'd still have to change <div> to &lt;div&gt; for it to render properly anyway - using code/pree doesn't stop the code from rendering.

正如您所见,Markdown实际上解析HTML(至少是这些标签)。在降价之外,您仍然需要将

更改为< div>无论如何都要正确呈现 - 使用code / pree不会阻止代码呈现。