show HTML as plain text

2019-07-23 05:12发布

问题:

im looking for a way to show html as html without the browser reading it, i found <plainttext> but once i start it i can't stop it for example:

<plaintext>
    <span> dobeediedabiedadadee olleeeeee</span>
</plaintext>
<h1>hi</h1>

in this example the span had to be shown as text and the h1 as a header, but the output is:

    <span> dobeediedabiedadadee olleeeeee</span>
</plaintext>
<h1>hi</h1>

</body>


</html>

here a JSFiddle link: JSFiddle

a other solution as plaintext is also welcome thanks for your time.

回答1:

plaintext has long been deprecated, just use &gt; and &lt;

&lt;span&gt; dobeediedabiedadadee olleeeeee&lt;/span&gt;

DEMO: Fiddle



回答2:

The following link describes the difficulty in using <plaintext>. long story short it is not fully supported in any browser and you should be using <pre> instead.

http://reference.sitepoint.com/html/plaintext



回答3:

You could always use javascript to escape the HTML. Here is a fiddle.

html.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");


回答4:

<plaintext> is not a pair tag. From that tag to the rest of the page, everything is interpreted as text. However, this is not standard and obsolette in HTML5:

Examples No, really. don't use it.

It is literally written in the w3 reference



回答5:

Use PRE

<pre>
    <span> dobeediedabiedadadee olleeeeee</span>
</pre>
<h1>hi</h1>


回答6:

it is compatibility issue some browser completely ignores coding of

have a look at this link http://reference.sitepoint.com/html/plaintext

i would suggest you use instead