渲染HTML中的字符串并保留空格和换行符

我有一个带有详细信息页面的MVC3应用程序。作为其一部分,我有一个描述(从数据库检索),其中有空格和换行符。呈现时,新行和空格将被html忽略。我想对这些空格和换行进行编码,以免被忽略。

你是怎样做的?

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


炎炎设计
浏览 970回答 3
3回答

慕雪6442864

只需使用设置样式white-space: pre-wrap;。div {&nbsp; &nbsp; white-space: pre-wrap;}<div>This is some text&nbsp; &nbsp;with some extra spacing&nbsp; &nbsp; and afew newlines along with some trailing spaces&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;and five leading spaces thrown infor&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; goodmeasure&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div>

UYOU

我正在尝试white-space: pre-wrap;pete所说的技术,但是如果字符串是连续的并且很长一段时间,它刚从容器中用完了,并且由于任何原因都没有翘曲,那么就没有太多的时间来研究 ..但是如果您也有同样的问题,我最终使用了<pre>标签和以下CSS,一切都很好。pre {font-size: inherit;color: inherit;border: initial;padding: initial;font-family: inherit;}
打开App,查看更多内容
随时随地看视频慕课网APP