简体   繁体   English

Javascript 换行文本区域

[英]Javascript Line Break Textarea

I have a text area that I want to pre-populate with some specific text that includes line breaks.我有一个文本区域,我想用一些包含换行符的特定文本预先填充该区域。 I'm populating the area onLoad but I can't get the line breaks to work correctly.我正在填充 onLoad 区域,但我无法让换行符正常工作。 Any advice on how to do that?关于如何做到这一点的任何建议? Thanks.谢谢。

您需要用换行符替换换行符: \\n

Just use the newline character: \\n .只需使用换行符: \\n

So, if you want the textarea to look like this:所以,如果你想让 textarea 看起来像这样:

This is line 1这是第 1 行
This is line 2这是第 2 行

You would use a string like this:您将使用这样的字符串:

"This is line 1\nThis is line 2";

See a demo here: http://jsfiddle.net/MzmBd/在此处查看演示: http : //jsfiddle.net/MzmBd/

As stated multiple times, you need the \\n character.正如多次所述,您需要\\n字符。

see here: http://jsfiddle.net/XbALv/见这里: http : //jsfiddle.net/XbALv/

document.getElementById("blah").value = 
"This" + "\n" +
"is some" + "\n" +
"text" + "\n";

i had tried many ways to break a textarea into an array.我尝试了很多方法将 textarea 分解为数组。 used this method, it might not be the best.使用这种方法,它可能不是最好的。

using .split('\\n') does not remove the break and it will create whitespace or break when insert into database.使用 .split('\\n') 不会删除中断,它会在插入数据库时​​创建空格或中断。 therefore, i replace it with因此,我将其替换为

textarea content:文本区域内容:
12345 12345
6789 6789


a = a.replace(/\n|\r\n|\r/g, "<br>");

var stringArray = a.split('<br>');

for (var i = 0; i < stringArray.length; i++) {
var myString = stringArray[i];
myString = myString.replace('<br>', "");

response.write(myString);
}

像这样替换:

str.replace(/[\r\n]+/g, '\\\n') %>';

break a textarea into an array将 textarea 分解为数组

textarea content: underscore_case first_name Some_Variable calculate_AGE delayed_departure textarea 内容: underscore_case first_name Some_Variable calculate_AGE delay_departure

   const variableCase = function (variable) {
        const variableSplit = variable.split('\n');
        const spaceRemoved = [];

        for (const removeSpace of variableSplit) {
          const RST = removeSpace.trim();
          spaceRemoved.push(RST);
        }

        console.log(spaceRemoved); // output: ["underscore_case", "first_name", "Some_Variable", "calculate_AGE", "delayed_departure"]
      };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM