HTML完全删除并忽略了我从JSON响应中获取的字符串中的控制字符

时间:2013-03-14 12:34:00

标签: javascript html json

我有一点问题。

我在JSON响应中得到一个String,其中包含控制字符(\ n)。

"ClData":" stream TV and films online. O\u0027Dwyer is accused of being the    administrator of the site. \n\nStudent O\u0027Dwyer was arrested on 23 May and spent the night in custody, before his aunt posted bail of £3,000."

现在我使用

console.log(root.CatLong.ClData)

我在控制台中收到格式化文本。

但如果我使用

content.innerHTML = root.CatLong.ClData

它显示没有“\ n”但完全没有格式化。

我的错误或.innerHTML是否无法解释控制字符。

提前致谢 最大

2 个答案:

答案 0 :(得分:2)

就浏览器而言,“\ n”只是空白区域,将被忽略。尝试将文本放在预标签中,告诉浏览器它是预先格式化的。

content.innerHTML = "<pre>" + root.CatLong.ClData + "</pre>";

答案 1 :(得分:0)

我必须将\ n替换为<br>,就像这个删除了他的答案所指出的人一样

content.innerHTML = root.CatLong.ClData.replace((/\n/g,"<br>");