有没有一种 HTML/CSS 方式可以在不解析的情况下显示 HTML 标签? [英] Is there a HTML/CSS way to display HTML tags without parsing?

查看:36
本文介绍了有没有一种 HTML/CSS 方式可以在不解析的情况下显示 HTML 标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以不解析就显示 HTML 标签?像 XMP 这样的标签以前可以完美运行,但现在它被替换为不太酷的 PRE.看看这个例子:

Is there any way that I could display HTML tags without parsing? Tags like XMP worked before perfectly but now it's replaced with PRE that isn't so cool. Take a look at this example:

//This used to NOT PARSE HTML even if you used standard < and >.
<XMP>
<a hred="http://example.com">Link</a>
</XMP>

//New PRE tag requires &lt; and &gt; as replacement for < and >.
<PRE>
&#60;a href="http://example.com"&#62;Link&#60;/A&#62;
</PRE>

我正在寻找的相当于旧的 XMP 标签.新的 PRE 标签将解析代码.

What I'm looking for is equivalent of old XMP tag. New PRE tag will parse code.

推荐答案

您可以使用 script 元素的 type 设置来表示纯文本,并设置它的 display 属性到 block.这仅影响解析行为:除了元素本身的结束标记 </script> 之外,不识别任何标记(标记或实体或字符引用).(所以它与xmp完全不一样,其中识别的标签是</xmp>.)你可以单独制作空格处理类似于 xmppre 和/或默认情况下将字体设置为这些元素中的等宽字体.

You can use a script element with its type set to denote plain text, and set its display property to block. This only affects the parsing behavior: no markup (tags or entity or character references) is recognized, except for the end tag of the element itself </script>. (So it is not quite the same as xmp, where the recognized tag is </xmp>.) You can separately make white space handling similar to that of xmp and pre and/or set the font the monospace as in those elements by default.

示例:

<style>
    script {
        display: block;
    }
</style>

然后在文档正文中:

<script type="text/plain">
    <i>&eacute;</i>
</script>

在最新版本的 IE、Chrome、Firefox、Opera 上测试.在 IE 8 和 IE 7 仿真中无法在 IE 9 上工作,但这可能是仿真中的一个错误.

Tested on newest versions of IE, Chrome, Firefox, Opera. Didn’t work in IE 8 and IE 7 emulation on IE 9, but that’s probably a bug in the emulation.

然而,我不明白你为什么要使用它而不是 xmp,它没有停止工作.它不在规格中,但如果您对此感到担心,那么您应该一直担心.在 HTML 2.0(有史以来的第一个 HTML 规范)中提到它是可以避免的,它在 HTML 3.2 中被弃用,并在 HTML 4.0 中完全删除(很久以前:1997 年).

However, I don’t see why you would use this instead of xmp, which hasn’t stopped working. It’s not in the specs, but if you are worried about that, you should have always been worried. Mentioned in HTML 2.0 (the first HTML spec ever) as avoidable, it was deprecated in HTML 3.2 and completely removed in HTML 4.0 (long ago: in 1997).

xmp 正在卷土重来而不是消亡.W3C HTML5(被 W3C 工作人员称为当前的 HTML 规范)声明 xmp 已过时且不符合标准,但它也强加了 要求 在浏览器上:用户代理必须以等效的方式处理 xmp 元素pre 元素在语义方面和呈现的目的.(不过,解析器对这个元素有特殊的行为.)"因此,旧的解析行为不是明确要求的,而是明确暗示的.

The xmp is making a comeback rather than dying. The W3C HTML5 (characterized as the current HTML specification by W3C staff) declares xmp as obsolete and non-conforming, but it also imposes a requirement on browsers: "User agents must treat xmp elements in a manner equivalent to pre elements in terms of semantics and for purposes of rendering. (The parser has special behavior for this element though.)" The old parsing behavior is thus not explicitly required, but clearly implied.

这篇关于有没有一种 HTML/CSS 方式可以在不解析的情况下显示 HTML 标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆