简体   繁体   English

在HTML文本区域添加换行符

[英]Add a line break to a html text area

I a trying to add a line break to a text area at run time (\\n) but instead of displaying as a line break the text area physically puts in \\n. 我试图在运行时(\\ n)将换行符添加到文本区域,而不是将换行符显示为实际放在\\ n的文本区域。 I have read various topics about this Link 1 and link 2 without any success. 我已经阅读了有关此链接1链接2的各种主题,但均未成功。 It could be down to the way the data is updated within the text area but that shouldn't matter. 这可能取决于在文本区域内更新数据的方式,但这无关紧要。 My data is brought in through json from a php file and displayed through jQuery 我的数据通过php文件中的json引入,并通过jQuery显示

Example of json response: json响应示例:

{"error":"2","err_msg":"","data":"<p class=\"left\">Aligned left text<\/p><br>Sub text <br>"}

The text in the text area should then display: 然后,文本区域中的文本应显示:

[left]Aligned left text[/left]
Sub text

But on my page I get 但是在我的页面上

[left]Aligned left text[/left]\nSub text

I have php function to convert relevent html tags to what I want them but for some reason line breaks don't work 我有php函数将relevent html标记转换为我想要的标记,但由于某些原因,换行符不起作用

$string = preg_replace_callback('(\<p class="left"\>(.*?)\</p\>)is', function($matches){
   $txt = $matches[1];
   return '[left]'.$txt.'[/left]\n';
}, $string);

$string = str_replace('<br>','\n',$string);

die(json_encode(array('error' => '2', 'err_msg' => '', 'data' => $string)));

There is a lot more formatting and different tags but its only the line break that doesn't work. 还有更多的格式和不同的标签,但只有换行符不起作用。

This change should be display in a text area 此更改应显示在文本区域中

$('#edit').val(json.data)
<textarea id="edit" ></textarea>

Try using the HTML entities for line feed &#10; 尝试将HTML实体用于换行&#10; and carriage return &#13; 和回车&#13; .

Don't use string literals encapsulated with apostrophes, try: 不要使用带撇号封装的字符串文字,请尝试:

$string = str_replace("<br>", "\\n", $string)

There is a significant difference between 'this string' and "this string" in PHP; 之间有显著差异'this string'"this string"在PHP; using apostrophes to encapsulate your string in PHP will treat \\n literally as \\n rather than chr(10) . 使用撇号将您的字符串封装在PHP中会将\\n视为\\n而不是chr(10)

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

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