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>&nbsp;</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

更多相关文章

  1. 9.1.4 前端 - HTML body标签 - 标题,段落,分割线,换行,特殊符号,列
  2. 更改所选文本的背景颜色,HTML JQUERY
  3. 在PHP中获取幕布元素ID的文本[重复]
  4. Server.Htmlencode用于文本域显示带的html代码
  5. IOS学习之WebView加载本地HTML代码或网络资源
  6. html5 css3 背景视频循环播放代码
  7. HTML5 标签audio添加网页背景音乐代码
  8. 我无法在某些Web浏览器中输入我的注册文本字段
  9. 将文本从表单复制到另一个网站的文本字段

随机推荐

  1. Android如何设置圆角按钮 类似微信的登陆
  2. Android学习路线
  3. JPCT-AE for Android 3D (一)----------H
  4. Android 进程间通信:AIDL
  5. 前端h5与 Android/iOS 交互传参
  6. [Android]如何获得heap的大小
  7. Android 利用drawable中的gradient属性实
  8. android的binder机制研究二
  9. Android、iOS系统架构
  10. Android(安卓)Build System ---- how to