猿问

使用`\ n`在html中换行

我觉得我问的是一个非常菜鸟的问题)有没有办法让HTML正确处理\n换行符?或者我必须替换它们<br/>?


https://jsfiddle.net/zawyatzt/



慕标琳琳
浏览 17734回答 3
3回答

LEATH

这是为了在html中显示新行和返回载体,然后您不需要显式地执行它。您可以通过设置white-space属性前置行值在css中执行此操作。<span style="white-space: pre-line">@Model.CommentText</span>

偶然的你

您可以使用CSS white-space属性\n。您还可以将标签保留为\t。换行\n:white-space: pre-line;对于换行符\n和制表符\t:white-space: pre-wrap;document.getElementById('just-line-break').innerHTML = 'Testing 1\nTesting 2\n\tNo tab';document.getElementById('line-break-and-tab').innerHTML = 'Testing 1\nTesting 2\n\tWith tab';#just-line-break {&nbsp; white-space: pre-line;}#line-break-and-tab {&nbsp; white-space: pre-wrap;}<div id="just-line-break"></div><br/><div id="line-break-and-tab"></div>
随时随地看视频慕课网APP
我要回答