有没有办法使HTML正确处理\n换行符?或者我必须用<br/>代替它们?

< div class = " text " > 美国广播公司 def 全球健康行动计划 < / div >


当前回答

您可以使用以下任何CSS,

white-space: pre-line;

or

white-space: pre-wrap;

or

white-space: break-spaces;

更多信息请阅读:https://developer.mozilla.org/en-US/docs/Web/CSS/white-space

其他回答

你可以使用<pre>标签:

<div class=“text”> <pre> 美国广播公司 定义 吉 </pre> </div>

使用空白:pre-line允许您使用换行符直接在HTML中输入文本,而不必使用\n

如果你通过JavaScript在非前置元素上使用元素的innerText属性,例如<div>,默认情况下,\n值将在DOM中被<br>替换

innerText:将\n替换为<br> innerHTML, textContent:需要使用样式留白

这取决于你如何应用文本,但有很多选择

const node = document.createElement('div');
node.innerText = '\n Test \n One '

简单而线性:

 <p> my phrase is this..<br>
 the other line is this<br>
 the end is this other phrase..
 </p>

您可以使用<pre>标记

< div class = " text " > < >之前 美国广播公司 def 全球健康行动计划 < / >之前 美国广播公司 def 全球健康行动计划 < / div >

你可以使用CSS属性

white-space: pre-line