本文介绍了如何在PRE或类似的东西中显示原始的html代码,但不会转义它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示原始HTML。我们都知道,每个人都必须逃避<和>像这样

 < PRE>这是一个测试& ltDIV& gt< / PRE> 

但是,我不想这样做。我想要一种方法来保持HTML代码的原样(因为它更易于阅读,(在编辑器内部),我可能想复制它并将其作为实际的HTML代码再次使用,并且不希望再次更改它,或者让同一个代码的两个版本逃脱,一个不逃脱)。

是否有任何其他环境比PRE更原始,可能允许这个?所以你不需要每次想要显示HTML代码时都要编辑HTML和改变一切,可能是HTML5?



类似于< REALLY_REALLY_VERBATIM> ......< /< REALLY_REALLY_VERBATIM>



屏幕截图



JavaScript解决方案在FF 21上无法使用,这里是屏幕截图
$ b

截屏2

第一个解决方案仍然不能在Firefox上运行,这里是屏幕截图

$您可以使用 xmp 元素,请参阅。它从一开始就一直在HTML中,并且受到所有浏览器的支持。虽然它也告诉作者不要使用它,但它并不能真正阻止你)。

> xmp 中的所有内容都是这样的,除此之外,没有任何标记(标记或字符引用)被识别,除了由于明显的原因,元素本身的结束标记。



否则 xmp 当使用真正的XHTML,即XHTML与XML媒体类型(很少见)一起服务时, ,特殊解析规则不适用,因此 xmp 被视为 pre 。但在真正的XHTML中,您可以使用CDATA部分,这意味着类似的解析规则。它没有特殊的格式,所以你可能想把它包装在 pre 元素中:

 < pre><![CDATA [
这是一个演示,标签类似于< p>会
字面上出现。
]]>< / pre>

我不知道如何将 xmp 和CDATA部分来实现所谓的多点标记

I'd like to display raw HTML. We all know one has to escape each "<" and ">" like this

     <PRE> this is a test  &ltDIV&gt </PRE>

However, I do not want to do this. I'd like a way to keep the HTML code as is (since it is easier to read, (inside the editor) and I might want to copy it and use it again myself as actual HTML code, and do not want to have to change it again or have 2 versions of the same code one escaped and one not escaped).

Is there any other environment that is more "raw" than PRE that might allow this? So one does not have to keep editing HTML and changing everything each time they want to show some raw HTML code, may be in HTML5?

Something like <REALLY_REALLY_VERBATIM> ...... </<REALLY_REALLY_VERBATIM>

screen shot

The javascript solution does not work on FF 21, here is screen shot

screen shot 2

The first solution still does not work on firefox, here is screen shot

解决方案

You can use the xmp element, see What was the <XMP> tag used for?. It has been in HTML since the beginning and is supported by all browsers. Specifications frown upon it, but HTML5 CR still describes it and requires browsers to support it (though it also tells authors not to use it, but it cannot really prevent you).

Everything inside xmp is taken as such, no markup (tags or character references) is recognized there, except, for apparent reason, the end tag of the element itself, </xmp>.

Otherwise xmp is rendered like pre.

When using "real XHTML", i.e. XHTML served with an XML media type (which is rare), the special parsing rules do not apply, so xmp is treated like pre. But in "real XHTML", you can use a CDATA section, which implies similar parsing rules. It has no special formatting, so you would probably want to wrap it inside a pre element:

<pre><![CDATA[
This is a demo, tags like <p> will
appear literally.
]]></pre>

I don’t see how you could combine xmp and CDATA section to achieve so-called polyglot markup

这篇关于如何在PRE或类似的东西中显示原始的html代码,但不会转义它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 12:19