\ n不是在javascript中的文本之间创建新行 [英] \n is not creating a new line between text in javascript

查看:81
本文介绍了\ n不是在javascript中的文本之间创建新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段JavaScript应该在我的HTML中更新:

I have a piece of JavaScript that is supposed to update a in my HTML:

var StringContent = ({
    "a": 'Some String a',
    "b": 'Some string b',
    "c": 'Some string c',
});

然后我想通过以下方式在新行上显示每个字符串a,b,c:

Then I want each string a, b, c displayed on a new line via:

document.getElementById("overlaycontent").innerHTML = (
    StringContent.a + '\n' +
    StringContent.b + '\n' +
    StringContent.c,
    )



<我现在得到的只是一条线。如何在我的文本中创建新行而不添加更多 div s?我也试过 \ r ,但这也无济于事。我查看了文档,但它一直在说使用 \ n

All I get at the moment is everything in a single line. How do I create a new line in my text without adding more divs? I also tried \r, but that also does not help. I looked at the docs, but it keeps saying to use \n.

推荐答案

您应该将 \ n 替换为< br> ,因为 innerHTML 接受一个HTML字符串(在HTML中, \ n 与相邻空格合并,但除了样式<$ c的元素外,不会产生回车符$ c> white-space:预包装,如MaxArt所述):

You should replace \n by <br> since innerHTML takes an HTML string (in HTML, \n merges with adjacent spaces but does not produce a carriage return except for elements with the style white-space:pre-wrap as noted by MaxArt) :

document.getElementById("overlaycontent").innerHTML = (
    StringContent.a + '<br>' +
    StringContent.b + '<br>' +
    StringContent.c,
)

这篇关于\ n不是在javascript中的文本之间创建新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆