簡體   English   中英

替換新行到 <br> ,不以字符串開頭,

[英]Replace new lines to <br>, without start of string,

我正在嘗試將Java腳本中的新行替換為<br> 但...

$(this).html($(this).text().replace(/(\n|\r)/gm, "<br>")); 

其他功能也像這樣替換第一行。 因此,我在一開始就使用Enter鍵來格式化文本。

<br> (?)
<br> (?)
Text
<br>

如何解決這個問題?

$(".class").each(function(){
    $(this).html($(this).text().replace(/\\n/g, "<br/>"));
}); 

您可能需要從字符串中刪除前導“ \\ n”,如下所示:

$(".class").each(function() {
    $(this).html( $(this).text().trim('\n').replace(/\n/g, "<br/>") );
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM