用javascript填充textarea,添加换行符

我正在尝试使用 javascript 填充 html 中文本区域的值。我下面的脚本基本上就是这样做的,但是我试图弄清楚如何在我的值中插入换行符(在“日期”和“文本”之间)。


基本上,正如所写,我的脚本给出了这个结果......


12月25日你回复:圣诞快乐!


然而,我希望它读作...


12月25日你回复:

圣诞快乐!


<script>

function myFunction() {

  document.getElementById("myTextarea").value = `On ${date} you replied: ${text}`;

}

</script>


冉冉说
浏览 63回答 2
2回答

跃然一笑

只需在字符串中添加 \n 即可将其推入新行。&nbsp; <script>&nbsp; &nbsp; let date= "11/01/2020";&nbsp; &nbsp; let text= "Merry Christmas!";&nbsp; &nbsp; function myFunction() {&nbsp; &nbsp; &nbsp; document.getElementById("demo").value= `On ${date} you replied: \n${text}`;&nbsp; &nbsp; }&nbsp; &nbsp; </script>

梵蒂冈之花

简单添加新的换行序列字符:\n<script>function myFunction() {&nbsp; document.getElementById("myTextarea").value = `On ${date} you replied: \n${text}`;}</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5