在HTML中呈现字符串并保留空格和换行符 [英] Render a string in HTML and preserve spaces and linebreaks

查看:478
本文介绍了在HTML中呈现字符串并保留空格和换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有详细信息页面的MVC3应用程序。作为我的一部分,我有一个描述(从数据库检索),有空格和新行。当它被渲染时,新的行和空格被html忽略。我希望对这些空格和新行进行编码,以免它们被忽略。



你怎么做的?



我尝试了HTML.Encode,但它最终显示了编码(甚至不是在空格和新行上,而是在一些其他特殊字符上)

解决方案

工作小提琴


I have an MVC3 app that has a details page. As part of that I have a description (retrieved from a db) that has spaces and new lines. When it is rendered the new lines and spaces are ignored by the html. I would like to encode those spaces and new lines so that they aren't ignored.

How do you do that?

I tried HTML.Encode but it ended up displaying the encoding (and not even on the spaces and new lines but on some other special characters)

解决方案

Just style the content with white-space: pre-wrap;.

Working fiddle

这篇关于在HTML中呈现字符串并保留空格和换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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