JavaScript新线构建字符串?
我正在在JavaScript Fe中构建一个字符串,就像您在下面看到的那样,我的尝试是在不同的行中打印一些数据。 在我的JavaScript中,我构建字符串使用getElement()
和textContent
在段落上附加字符串。
我已经尝试过< br>
< br/>
\ n
< \ r
没有结果。
var str;
str+="text" + data[0];
str+= //Here need new line
str+="text" + data[1];
var p=document.getElementById("paragraph");
p.textContent = str;
I'm building a string in JavaScript FE like you can see below, my attempt is to print some data in different rows.
In my JavaScript I build the string the use getElement()
and textContent
to attach the string at the paragraph.
I've tried <br>
<br/>
\n
<\r
, all with no results.
var str;
str+="text" + data[0];
str+= //Here need new line
str+="text" + data[1];
var p=document.getElementById("paragraph");
p.textContent = str;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用然后使用
innertext
而不是textContent
作为不同。It might be easier to use a template string, and then use
innerText
rather thantextContent
as they are different.您拥有的几个选项是:
&lt; br/&gt;
放在字符串中,并设置p.innerhtml = str
而不是设置TextContent或
\ n
字符,然后在元素的CSS中使用pree
A couple options you have are:
<br/>
in the string and set thep.innerHTML = str
instead of setting textContentOR
\n
character in the string and then use awhite-space: pre
in the CSS of your element如果您需要一个可以以文件显示/下载并同时显示在HTML中的字符串,我将使用
\ n
和innertext
:\ \ code> \ n
将在使用innertext时自动替换&lt; br/&gt;
,并且您不需要用whitespace
来造型,并且您可以使用结果字符串,也许启动文件下载If you need a string that can be displayed/downloaded as a file and displayed in html at the same time, i would use
\n
andinnerText
:the
\n
will be replaced by<br/>
automatically when using innerText, and you wont need to style it withwhitespace
, and you could use the resulting string, to perhaps start a file download