Is there a HTML/CSS way to display HTML tags without parsing?

前端 未结 9 1105
忘了有多久
忘了有多久 2020-12-05 23:25

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

相关标签:
9条回答
  • 2020-12-06 00:25

    And then... a few years go by, I have the same problem while converting my blog from wordpress to a vuejs spa backed by lambda and dynamodb.

    And the answer is; at least in my situation. Escape the entity.

    < becomes <

    > becomes >

    etc. etc.

    Hope this helps.

    0 讨论(0)
  • 2020-12-06 00:27

    Technically you could use <textarea>, but it would require that there be no </textarea> tag in the code you are trying to show. It'd just easier to escape the <.

    0 讨论(0)
  • 2020-12-06 00:29

    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.

    Example:

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

    Then within document body:

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

    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.

    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).

    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.

    0 讨论(0)
提交回复
热议问题