[英]Add <br /> tag after end of every line in jQuery / JavaScript
I have a <textarea>
field for user input. 我有一个
<textarea>
字段供用户输入。 I want this - When user paste their text in that field and click some button it will automatic add a line break tag <br />
after the end of every line. 我想要这样-当用户将其文本粘贴到该字段中并单击某个按钮时,它将在每行末尾自动添加一个换行标记
<br />
。
My html form look like this. 我的html表单如下所示。
<div class="comment"> <textarea class="form-control page_details" rows="5" id="text" placeholder="Detail here" name="details"></textarea> <button type="button">Generate</button> </div>
and when user paste their text on textarea field like. 当用户将其文本粘贴到textarea字段上时。
Lorem ipsum dolor sit amet,
Lorem ipsum dolor坐在amet,
consectetuer adipiscing elit,保密专家,
sed diam nonummy nibh euismod tinciduntsed diam nonummy nibh euismod tincidunt
ut laoreet dolore magna aliquamut laoreet dolore magna aliquam
erat volutpat.擦除volutpat。
When clicking the button it will look like this. 当单击按钮时,它将看起来像这样。
Lorem ipsum dolor sit amet,
<br />
Lorem ipsum dolor坐在amet,
<br />
consectetuer adipiscing elit,<br />
consectetuer adipiscing精英,
<br />
sed diam nonummy nibh euismod tincidunt<br />
sed diam nonummy nibh euismod tincidunt
<br />
ut laoreet dolore magna aliquam<br />
ut laoreet dolore magna aliquam
<br />
erat volutpat.擦除volutpat。
<br />
Help me out guys. 帮帮我。
You could use String.protototype.split()
with RegExp
/\\n|\\s\\n/
, Array.prototype.join()
with parameter "<br>\\n"
, concatenate "<br>"
to end of replacement textarea
value
您可以将
String.protototype.split()
与RegExp
/\\n|\\s\\n/
,将Array.prototype.join()
与参数"<br>\\n"
,将"<br>"
连接到替换textarea
末尾value
function addBreak(el) { var textarea = el; var matches = textarea.value.split(/\\n|\\s\\n/); textarea.value = matches.join("<br>\\n") + "<br>"; }
<div class="comment"> <textarea class="form-control page_details" rows="5" id="text" placeholder="Detail here" name="details"></textarea> <button type="button" onclick="addBreak(this.previousElementSibling)">Generate</button> </div>
Use on paste event. 在粘贴事件上使用。
$("button").on("click",function(){ $("#text").bind("paste", function(e){ $('#txtarea').append('<BR>').focus(); }); });
<div class="comment"> <textarea class="form-control page_details" rows="5" id="text" placeholder="Detail here" name="details"></textarea> <button type="button">Generate</button> </div>
I'd cover my bets by handling \\r\\n
(the sequence), and then handling \\r
and \\n
through a character class, like this: 我会先处理
\\r\\n
(顺序),然后再通过字符类处理\\r
和\\n
,以此来下注:
text = text.replace(/\r\n/g, '<br />').replace(/[\r\n]/g, '<br />');
The first replace turns the sequence \\r\\n
into <br />
. 第一个替换将序列
\\r\\n
转换为<br />
。 The second replace replaces any \\r
or \\n
characters found on their own with the string. 第二个替换用字符串替换自己找到的任何
\\r
或\\n
字符。
More on regular expressions in JavaScript here . 有关JavaScript中正则表达式的更多信息,请参见此处 。
Replace the new line characters with <br/>
用
<br/>
替换换行符
var pastedText = $("#text").val();
pastedText = pastedText.replace(/(\r\n|\n|\r)/gm, "<br>");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.