简体   繁体   English

如果附加带有发布的html的div,我如何保持换行符不变?

[英]How do I keep the line breaks intact, when appending a div with posted html?

步骤1 2 3

Below and above are the 3 steps of what happens. 以下是发生的3个步骤。

var newcontent = $("#commenthere").val();
alert(newcontent)
$('#commentarea').replaceWith(newcontent);

What I want is when the content is appended/replaced, whatever, that it displays 我想要的是当内容被添加/替换时,无论它显示什么

1 1

2 2

on the page instead of 1 2 在页面而不是1 2

I tried replacing \\n with <br> using replace , but it doesn't work as it causes syntax errors as it moves the javascript code to a new line, breaking the function. 我试图替换\\n<br>使用replace ,但它不工作,因为它会导致语法错误,因为它移动的javascript代码到一个新的线,打破了功能。

设置div的样式white-space: pre应该解决这个问题。

This works for me: 这对我有用:

str = str.replace( '\n', '<br>' );

Live demo: http://jsfiddle.net/QvFdy/1/ 现场演示: http //jsfiddle.net/QvFdy/1/

听起来你需要一个<pre />标签,它会格式化文本,包括所有空白区域。

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

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