在转义的html中渲染换行符 [英] Rendering newlines in escaped html

查看:128
本文介绍了在转义的html中渲染换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个默认自动生成的显示消息,但可能会被来自url的GET变量覆盖。由于我们需要将消息视为用户输入,因此必须转义才能显示。但是,我们希望保留包含换行符的功能。



换行符为< br>

这不起作用,因为转义HTML会破坏< br> 标记。

换行符为 \\\


我可以'弄清楚如何让 \\\
呈现为换行符。我认为把它放在标签中会正确呈现,但没有运气: http://jsfiddle.net/7L932/

解决方案

你在做什么或多或少都很好,除了你应该把 \ n 字符(换行符),而不是html中的转义序列(Prinzhorn所说的也是非常有意义的,所以我会为他付出代价)。


We have a display message that is auto-generated by default, but may be overridden by GET vars from the url. Since we need to treat the message as user input, it must be escaped for display. However, we want to retain the ability to include newlines.

Newlines as <br>
This won't work because escaping HTML destroys the <br> tag.

Newlines as \n
I can't figure out how to get \n to render as newlines. I thought putting it in a tag would render correctly, but no luck: http://jsfiddle.net/7L932/

解决方案

What you're doing is more or less fine, except for you should put \n character (newline), not the escape sequence in your html (and what Prinzhorn says also makes perfect sense, so I'll go upvote him).

这篇关于在转义的html中渲染换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆