Can you make newline characters \n display as breaks <br />?

怎甘沉沦 提交于 2019-12-12 07:44:57

问题


I'm trying to display emails with IMAP. When the email is text though, the newlines \n don't display properly. I'd rather not convert them to breaks < br /> because if the user wanted to reply to that email, it would now be in HTML instead of plain text. Is there perhaps a javascript function that could display it as a line break without changing the code?


回答1:


How about HTML/CSS? If you put your text inside a <pre> tag, it will show all newlines exactly as they were. Alternatively, you can achieve the same effect by applying the CSS style white-space:pre to any element.

Don't forget to HTMLencode it still (< to &lt; etc.), otherwise it will all break apart at the first angle bracket.




回答2:


Just add this white-space css style property to render Multiline texts :

.multiline
{
   white-space: pre-wrap;
}

and then :

<div class="multiline">
  my
  multiline
  text
</div>

now newlines will render like br elements.




回答3:


white-space CSS works fine but for cross-browser compatibility

.abc {
  word-wrap: break-word;      /* IE 5.5-7 */
  white-space: pre-wrap;      /* Modern browsers */
}

Your Html

<div class="abc">
 Lorem 
 Ipsum 
 is 
 simply 
 dummy
</div>

MDN Source



来源:https://stackoverflow.com/questions/13118738/can-you-make-newline-characters-n-display-as-breaks-br

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!