86
4

11 に答える 11

108

Unfortunately it doesn't work with HTML tags.

<code> means "This is code", <pre> means "White space in this markup is significant". Neither means "The content of this element should not be treated as HTML", so both work perfectly, even if they don't mean what you want them to mean.

Is there any way to show "<div>" inside of <pre> or <code> tag without actually interpreting it as HTML?

If you want to render a < character then use &lt;, with &gt; for > and &amp; for &.

You can't (in modern HTML) write markup and have it be interpreted as text.

于 2012-07-08T20:50:24.643 に答える
55

It seems like a readonly textarea does pretty much what you want.

<textarea readonly> <!-- html code --> </textarea>
于 2015-08-07T15:48:54.607 に答える
32

You can use the "xmp" element. The <xmp></xmp> has been in HTML since the beginning and is supported by all browsers. Even it should not be used, it is broadly supported.

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

Otherwise "xmp" is rendered like "pre".

于 2016-10-06T15:42:18.873 に答える
18

Try:

<xmp></xmp>

for exemple:

<pre>
     <xmp><!-- your html code --></xmp>
</pre>

bye

于 2017-04-13T18:16:19.390 に答える
10

Just escape the HTML tags. For example -

Replace < with &lt;

Replace > with &gt;

Complete lookup here

于 2016-12-13T13:20:29.817 に答える
4

This can be easily achieved with a little bit of javascript.

document.querySelectorAll("code").forEach(el => el.innerText = el.innerHTML);

Run snippet below to see it in action:

document.querySelectorAll("code").forEach(el => el.innerText = el.innerHTML);
pre {
  padding: 1rem;
  background-color: #eee;
  border-radius: 0.25rem;
}
<pre>
  <code>
   .class {        
     color:red;
   }
   
   // I would like HTML code inside this
   <h1>Hello this is a heading</h1>
  </code>
</pre>

于 2020-04-17T11:54:57.487 に答える
0

Try CodeMirror (https://codemirror.net/)

It's a lightweight library that styles code in HTML. Here's a screenshot of what I'm referring to:

enter image description here

Worked well for us!

于 2015-06-02T20:03:08.347 に答える
0

Maybe not the best answer, but you can do something like this:

<p><<input type="hidden">h1<input type="hidden">><input type="hidden">This code is 
displayed!<input type="hidden"><<input type="hidden">/h1<input type="hidden">></p>

<p><<input type="hidden">h1<input type="hidden">><input type="hidden">This code is displayed!<input type="hidden"><<input type="hidden">/h1<input type="hidden">></p>

于 2020-03-09T20:37:56.137 に答える
0

<pre>
  <code><textarea>
    <div>Now you can write Tags inside pre tag!</div>
  </textarea><code>
 <pre>

于 2021-10-27T08:09:49.547 に答える
-3

Use this:

<pre>
   <?= htmlentities($script) ?>
</pre>
于 2018-11-28T13:17:32.260 に答える
-4

Yes, with an escape xml function. You'll need to have jQuery enabled for it to work though.

<pre>
  ${fn:escapeXml('
    <!-- all your code -->
  ')};
</pre>
于 2014-12-17T23:18:45.667 に答える