我想在页面中添加一个区域,其中所有动态内容都呈现为纯文本而不是标记.例如:
<myMagicTag>
<b>Hello</b> World
</myMagicTag>
Run Code Online (Sandbox Code Playgroud)
我希望<b>标签只显示为文本而不是粗体指令.我宁愿不必编写代码来将每个"<"转换为" <".
我知道<textarea>会这样做,但它有其他不良副作用,如添加滚动条.
myMagicTag存在吗?
编辑:执行此操作的jQuery或javascript函数也可以.不幸的是,无法在服务器端做到这一点.
uno*_*nor 17
您可以使用script元素(由我加粗)执行此操作:
该
script元素允许作者在其文档中包含动态脚本和数据块.
例:
<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>
Run Code Online (Sandbox Code Playgroud)
(请注意,script元素的允许内容受到限制,例如,您不能将</script>文本内容作为文本内容(它会关闭script元素).)
通常,script元素display:none在浏览器的CSS中默认具有,因此您需要在CSS中覆盖它,例如:
script[type="text/plain"] {display:block;}
Run Code Online (Sandbox Code Playgroud)
不,这是不可能的,您需要对其进行 HtmlEncode。
如果您使用服务器端语言,那并不困难。
在 .NET 中你会做这样的事情:
string encodedtext = HttpContext.Current.Server.HtmlEncode(plaintext);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10121 次 |
| 最近记录: |