我有一个包含详细信息页面的 MVC3 应用程序。作为其中的一部分,我有一个包含空格和换行符的描述(从数据库中检索)。渲染时,新行和空格会被 html 忽略。我想对这些空格和新行进行编码,以便它们不会被忽略。
你是怎么做到的?
我尝试了 HTML.Encode,但它最终显示了编码(甚至不在空格和换行符上,而是在一些其他特殊字符上)
最佳答案
只需使用 white-space: pre-wrap;
设置内容样式即可.
div {
white-space: pre-wrap;
}
<div>
This is some text with some extra spacing and a
few newlines along with some trailing spaces
and five leading spaces thrown in
for good
measure
</div>
https://stackoverflow.com/questions/9492249/