HTML标记,导致其他标记呈现为纯文本 [英] HTML tag that causes other tags to be rendered as plain text
问题描述
我想在页面中添加一个区域,其中所有动态内容都呈现为纯文本而不是标记。例如:
I'd like to add an area to a page where all of the dynamic content is rendered as plain text instead of markup. For example:
<myMagicTag>
<b>Hello</b> World
</myMagicTag>
我想要< b>
标签显示为文本,而不是粗体指令。我宁愿不必编写代码来转换每个<到& lt;
。
I want the <b>
tag to show up as just text and not as a bold directive. I'd rather not have to write the code to convert every "<" to an "<
".
我知道< textarea>
会这样做,但它有其他不良副作用,比如添加滚动条。
I know that <textarea>
will do it, but it has other undesirable side effects like adding scroll bars.
myMagicTag是否存在?
Does myMagicTag exist?
编辑:执行此操作的jQuery或javascript函数也可以。不幸的是,无法在服务器端做到这一点。
A jQuery or javascript function that does this would also be ok. Can't do it server-side, unfortunately.
推荐答案
你可以用 script
元素(由我加粗):
You can do this with the script
element (bolded by me):
脚本
元素允许作者包含动态脚本和数据块在他们的文件中。
The
script
element allows authors to include dynamic script and data blocks in their documents.
示例:
<script type="text/plain">
This content has the media type plain/text, so characters reserved in HTML have no special meaning here: <div> ← this will be displayed.
</script>
(请注意脚本的允许内容
元素是限制,例如你< a href =http://www.w3.org/TR/html5/syntax.html#cdata-rcdata-restrictions>不能 < / script>
作为文本内容(它将关闭脚本
元素)。)
(Note that the allowed content of the script
element is restricted, e.g. you can’t have </script>
as text content (it would close the script
element).)
通常, script
元素在浏览器的CSS中默认为 display:none
,所以你需要在CSS中覆盖它,例如:
Typically, script
elements have display:none
by default in browser’s CSS, so you’d need to overwrite that in your CSS, e.g.:
script[type="text/plain"] {display:block;}
这篇关于HTML标记,导致其他标记呈现为纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!