[英]Is .value preventing line breaks in the textarea?
如何在模板中获得换行符以显示在文本区域中?
我正在尝试在每个模板后添加一个换行符,
并且由于使用.value
而无法使用</ br>
。
我尝试过JavaScript:如何在HTML textarea中添加换行符? 和javascript | 使用换行符保存textarea值,以及使用style="white-space:pre-wrap"
。
var template_Birthday = "Hey <NAME>, This is the Birthday Template."; var template_NewJob = "Hey <NAME>, This is the New Job Template."; var content = function() { var e = document.getElementById("template"); var strUser = e.options[e.selectedIndex].value; if (strUser === "template_NewJob") { messageBodyTextarea.value = template_NewJob; } else if (strUser === "template_Birthday") { messageBodyTextarea.value = template_Birthday; } };
<textarea class="form-control" rows="7" name="message" id="messageBodyTextarea"></textarea>
使用\\ n作为新行。
var template_NewJob = "Hey <NAME>,\nThis is the New Job Template.";
由于您似乎要设置textArea
的值,因此您是否尝试了转义的换行符:
messageBodyTextarea.value = "Hey <NAME>, \nThis is the Birthday Template.";
如果要向DOM编写HTML,则只需使用<br />
。
要将换行符添加到HTML中的textarea
:
<textarea id="myTextarea">Line 1
Line 2
Line 3</textarea>
要将换行符添加到JavaScript中的textarea
:
var ta = document.getElementById('myTextarea');
ta.value = 'Line1\nLine2\nLine3';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.