[英]Get text area value and store it in a div with line breaks using JavaScript
I need to get a text from Textarea element and save the text into a div keeping line breaks obtained by pressing Enter key. 我需要从Textarea元素中获取文本并将文本保存到div中,以保持按Enter键获得的换行符。 But the code I am using does not seem to be good enough :) Could you please take a look at the code below and share a suggestions on how to achieve such result:
但是我使用的代码似乎还不够好:)请您看一下下面的代码,并就如何实现这种结果分享建议:
JS JS
var div = $('div');
var textarea = $('textarea');
textarea.keyup(function(e){
if(e.keyCode === 13){
div.text(textarea.val() + "\n")
}
})
Result div: 结果div:
Result using 结果使用
instead of the \\n 而不是\\ n
You should use 你应该用
<br/>
instead of \\n
for new line in Html <br/>
而不是\\n
在HTML中换行
Also use .append()
instead of .text()
同时使用
.append()
而不是.text()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.