简体   繁体   English

javascript-在textarea中插入文本后添加换行符

[英]javascript- add linebreak after inserting text in textarea

Here is my JavaScript code . 这是我的JavaScript代码。 while inserting text in textbox i want to insert line break . 在文本框中插入文本时,我想插入换行符。

element = element +'<div class="msg_wrap"><div class="msg_body"><ul id="chatlog"></ul>'+ msg+'<div class="msg_push" id="textmesage">dg</div></div><div class="msg_footer" ><textarea id="myTextArea" class="msg_input" rows="4" onkeypress="PushMessage(event , this);"+"\n">  </textarea></div></div>';

and here is my function through which i get text through chatbox. 这是我通过聊天框获取文本的功能。

function PushMessage(e , textarea)
{
    var code = (e.keyCode ? e.keyCode : e.which);
    if(code == 13) { //Enter keycode
    var text1 = document.getElementById('myTextArea').value;
    var div = document.getElementById('textmesage');
    div.innerHTML = div.innerHTML + text1;
    var div = document.getElementById('textmesage').value = "<br>";
    document.getElementById('myTextArea').value = "";
}

i have tried +"\\n" and +" 我尝试过+“ \\ n”和+“
"+ and in place of " “ +代替”
" in 2ndlast line but it dsnt work. ”在第二个最后一行中,但它可以正常工作。

I'm not sure if I've understood you well. 我不确定我是否了解您。

If you want to add line break in the text you're inserting, you can use 如果要在要插入的文本中添加换行符,可以使用

< br > <br>

, as this: ,如下所示:

 function PushMessage(e , textarea) { var code = (e.keyCode ? e.keyCode : e.which); if(code == 13) { //Enter keycode var text1 = document.getElementById('myTextArea').value; var div = document.getElementById('textmesage'); div.innerHTML = div.innerHTML + '<br>' + text1; document.getElementById('myTextArea').value = ""; } } 
 <div class="msg_wrap"> <div class="msg_body"> <ul id="chatlog"></ul> <div class="msg_push" id="textmesage">dg</div> </div> <div class="msg_footer" > <textarea id="myTextArea" class="msg_input" rows="4" onkeypress="PushMessage(event , this);"></textarea> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM