How to show <div> tag literally in <code>/<pre> tag?

烈酒焚心 提交于 2019-12-17 02:29:34

问题


I'm using <code> tag inside of a <pre> tag to show code on my blogger blog. Unfortunately it doesn't work with HTML tags. Is there any way to show "<div>" inside of <pre> or <code> tag without actually interpreting it as HTML? This is what I do right now:

<pre>
 <code>
 .class {        
   color:red;
 }
 // I would like HTML code inside this
 </code>
</pre>

Which works ok for everything except HTML. Any idea how to achieve this? Thanks.


回答1:


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.




回答2:


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

<textarea readonly> <!-- html code --> </textarea>



回答3:


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




回答4:


Try:

<xmp></xmp>

for exemple:

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

bye




回答5:


Just escape the HTML tags. For example -

Replace < with &lt;

Replace > with &gt;

Complete lookup here




回答6:


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:

Worked well for us!




回答7:


Use this:

<pre>
   <?= htmlentities($script) ?>
</pre>



回答8:


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>


来源:https://stackoverflow.com/questions/11386586/how-to-show-div-tag-literally-in-code-pre-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!