When we submit data through textarea on the page, the content we input will be processed as a piece of content after submission, even if we input a lot of line breaks in textarea, after a piece of content is submitted, what should we do if we need to save the input information as input?
The main problem is that the line break is the <br/> label when the page is displayed, and the line break when the content is displayed in textarea is \ n, the following describes a method to convert a line break in the input content to a line break in the webpage through js. You can simply submit the money on the webpage for the following processing.
Copy codeThe Code is as follows:
<Script type = "text/javascript">
// Press enter to convert the line
Var content = document. getElementById ("content"). value;
Content = content. replace ('\ n',' <br/> ');
Document. getElementById ("content"). value = content;
// Then submit
</Script>
When displayed on the page, you must perform the following operations:
Copy codeThe Code is as follows:
Content = content. replace ('<br/>', '\ n ');
The following describes the replace function of javascript:
Syntax:
StringObject. replace (regexp/substr, replacement)
The replace () method is used to replace other characters with some characters in a string, or replace a substring that matches a regular expression.
1. Replace a single character
Copy codeThe Code is as follows:
<Script language = "javascript">
Var str = "javascript is a good script language ";
Alert (str. replace ("a", "A"); // output jAvascript is a good script language
</Script>
2. replace all specified characters in the string (replace all)
Copy codeThe Code is as follows:
<Script language = "javascript">
Var str = "javascript is a good script language ";
Alert (str. replace (/a/g, "A"); // output jAvAscript is A good script lAnguAge
</Script>
The above/g is a regular expression, which indicates that you can also use a regular expression in replace.
Of course, replace has more advanced usage. You can get more advanced usage in related documents.