any alternative of <xmp> tag to renders html to as plain text [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-23 12:05:18

问题


I want to show HTML code as plain text.
for example

<div style="width:250px;">
<xmp>
<a href="#"> new element  dummy text new element  dummy text new element  dummy text new element  dummy text new element  dummy text</a>
</xmp>
<div>

The problem is while using <xmp> tag its ignore the div width that contain text. How can I fix it out. Any other solution instead of <xmp> tag?


回答1:


Simply using <xmp> or <pre> tag won't help you display HTML as plain text. The just help to format the text. In order to show HTML to your users you need to escape all special characters.

For example, replace all < signs with &lt; and all > signs with &gt;.

I've shown just two symbols, a quick google search will help you to find all the symbols and their replacements.

And in case you're using PHP, it has built in function to do this. i.e. htmlentities($str). Search for whatever language you are using.

Note that <xmp> Tag has been deprecated.




回答2:


Instead of deprecated xmp you should use a script set to visible and data block.

<div style="width:250px;">
  <script type="text/plain" style="display:block;">
<a href="#"> new element  dummy text new element  dummy text new element  dummy text new element  dummy text new element  dummy text</a>
  </script>
<div>



回答3:


<div style="width:250px; background:yellow;">
<pre>
<script type="text/plain" style="display:block;">
<a href="#"> new element  dummy 
text new element  dummy text new 
element 
element 
dummy text new element 
dummy text new element 
dummy text</a>
</script>
</pre>
<div>


来源:https://stackoverflow.com/questions/34692479/any-alternative-of-xmp-tag-to-renders-html-to-as-plain-text

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