【问题标题】:json request - no line breaks in divjson 请求 - div 中没有换行符
【发布时间】:2011-11-29 10:52:22
【问题描述】:

我使用 json/jquery/ajax 将数据保存在数据库中。 当我在 textarea 中加载数据时,我得到了我想要的结果。 但是当我在 DIV 中加载数据时,没有换行符。 我用 css 测试了所有东西,包括

white-space:pre;

等等

我将数据保存在 json 中

"', 'text': '" + $('#textInput').val() +

但我也使用 .html() 和 .text() 对其进行了测试。

读取数据:

$('#textOutput').text(data.d.Text);

我在我的 div 中获得了正确的文本,但没有换行符!

文本输出的 CSS

.textOutput // <div> not working, <textarea> working
{
width:100%;
border: 0;
height: auto;
overflow:visible; // also tested with auto etc.
margin: 5px 8px 5px 0px;
font-family: Courier New, Courier;
font-size: 12px;
// white-space:pre;
}

输入的CSS是等价的

【问题讨论】:

  • 您的数据库使用什么? php?

标签: jquery ajax json line-breaks


【解决方案1】:

如果您使用的是 PHP,则必须调用 nl2br()\n\r 更改为 &lt;br /&gt; 标记。您也可以通过data.d.Text.replace(/\n/g, "&lt;br /&gt;");

在 Javascript 中执行此操作

对于 ASP,您可以使用.Replace("\n", "&lt;br /&gt;")

【讨论】:

  • 这是你需要的,但是 asp.net 的替代品
  • 它正在工作 - 谢谢! (答案可以/将在 3 分钟内被接受)
猜你喜欢
  • 2011-06-14
  • 1970-01-01
  • 1970-01-01
  • 2014-10-18
  • 1970-01-01
  • 1970-01-01
  • 2014-12-04
  • 1970-01-01
  • 2019-03-18
相关资源
最近更新 更多