jQuery:keyup():用文本区域的内容更新 div...换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6203470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:32:13  来源:igfitidea点击:

jQuery: keyup(): Update div with content from text area... line breaks?

jqueryline-breakskey-events

提问by superUntitled

I have posted a working version here: http://jsfiddle.net/JV2qW/2/

我在这里发布了一个工作版本:http: //jsfiddle.net/JV2qW/2/

I have a textarea that updates (on keyup()) a div with the text that is being entered. Everything is working as it should, except the line breaks are not being recognized.

我有一个文本区域,它keyup()使用正在输入的文本更新(在)一个 div。一切正常,除了无法识别换行符。

the html:

html:

<p>enter text</p>
<textarea id='text'></textarea>
<div id='target'></div>

and the jquery:

和jQuery:

$('#text').keyup(function(){
      var keyed = $(this).val();
      $("#target").html(keyed);
 });

Any thoughts on how to get the \ntranslated to <br/>or <p>tags?

关于如何获得\n翻译<br/><p>标签的任何想法?

many thanks.

非常感谢。

回答by Robert

You can replace any newlines with <br/>

您可以将任何换行符替换为 <br/>

$('#text').keyup(function() {
    var keyed = $(this).val().replace(/\n/g, '<br/>');
    $("#target").html(keyed);
});

You can look into the MDC article about RegEx if you want to replace other things.

如果您想替换其他内容,可以查看有关 RegEx 的 MDC 文章。

https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions

https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions

回答by Gergely Fehérvári

http://jsfiddle.net/omnosis/8XL7n/

http://jsfiddle.net/omnosis/8XL7n/

replace the '\n'to '<br />'

替换'\n''<br />'

回答by Piotr Perak

Why don't You just do replace on keyed value and replace it?

你为什么不只是替换键值并替换它?

Here You have example - section Convert carrage returns

在这里你有例子 - 部分转换回车

http://lawrence.ecorp.net/inet/samples/regexp-format.php

http://lawrence.ecorp.net/inet/samples/regexp-format.php