2015-11-24 124 views
0

我想使用JavaScript在我的HTML文本區域顯示輸出,但我無法弄清楚如何插入換行符。基於我發現這裏其他的答案,我想這一點:JavaScript文本區域:添加換行符

txtResults.value = "Route: " + route + "\\\n" + "Start Station: " + stnStart + "\\\n" + "End Station: " + stnEnd; 

,但輸出出來作爲

Route: A>B>C 
\ 
Start Station: A\ 
End Station: C 

,但如果不是\\\n我把\\n輸出出來作爲

Route: A>B>C 
\nStart Station: A\nEnd Station: C 

我如何使它如下所示?

Route: A>B>C 
Start Station: A 
End Station: C 
+0

怎麼樣「\ n」 –

+0

而不是隨機嘗試反斜槓,你爲什麼不如何逃脫在JavaScript字符串序列工作讀到的不同組合?從https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String開始。 –

回答

2

「\ n」能正常工作嗎?

var route = "A>B>C", stnStart="A" , stnEnd = "C"; 
 

 
document.getElementById("textarea").value = "Route: " + route + "\n" + "Start Station: " + stnStart + "\n" + "End Station: " + stnEnd;
<textarea id="textarea" style="width:100%;display:block" rows="5"></textarea>

相關問題