使用换行符将textarea的文本复制到div中
16lz
2021-01-22
I am tying to make a simple effect using keyup()
in jQuery. I just want that when user types into the textarea
then the text which user types will copy to another div named .content
. When I press enter in textarea a new line is created but in my div the text is showing in the same line. My code is below or you can see demo here: http://jsfiddle.net/Pqygp/
我想在jQuery中使用keyup()来制作一个简单的效果。我只是希望当用户输入textarea时,用户类型的文本将复制到另一个名为.content的div。当我在textarea中按Enter键时,会创建一个新行,但在我的div中,文本显示在同一行中。我的代码如下,你可以在这里看到演示:http://jsfiddle.net/Pqygp/
HTML
<textarea name="mas" rows="15" class="content"></textarea>
<p> </p>
<div class="mas" >Texts Comes here</div>
SCRIPT
$('.content:not(.focus)').keyup(function(){
var value = $(this).val();
var contentAttr = $(this).attr('name');
$('.'+contentAttr+'').html(value);
})
5 个解决方案
#1
13
You need to convert the literal newlines into <br>
tags for proper output in the DIV.
您需要将文字换行符转换为
标记,以便在DIV中正确输出。
$('.'+contentAttr+'').html(value.replace(/\r?\n/g,'<br/>'));
Shown in your code below:
在下面的代码中显示:
$('.content:not(.focus)').keyup(function(){
var value = $(this).val();
var contentAttr = $(this).attr('name');
$('.'+contentAttr+'').html(value.replace(/\r?\n/g,'<br/>')); //convert newlines into <br> tags
});
JSFiddle
更多相关文章
- 9.1.4 前端 - HTML body标签 - 标题,段落,分割线,换行,特殊符号,列
- 更改所选文本的背景颜色,HTML JQUERY
- 在PHP中获取幕布元素ID的文本[重复]
- Server.Htmlencode用于文本域显示带的html代码
- IOS学习之WebView加载本地HTML代码或网络资源
- html5 css3 背景视频循环播放代码
- HTML5 标签audio添加网页背景音乐代码
- 我无法在某些Web浏览器中输入我的注册文本字段
- 将文本从表单复制到另一个网站的文本字段