JavaScript新线构建字符串?

发布于 2025-01-27 08:13:10 字数 453 浏览 2 评论 0原文

我正在在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愁杀 2025-02-03 08:13:10

使用然后使用innertext而不是textContent作为不同

const arr = ['Bob', 'Jane'];

const str = `
  text: ${arr[0]}
  text: ${arr[1]}
`;

document.body.innerText = str;

It might be easier to use a template string, and then use innerText rather than textContent as they are different.

const arr = ['Bob', 'Jane'];

const str = `
  text: ${arr[0]}
  text: ${arr[1]}
`;

document.body.innerText = str;

江湖正好 2025-02-03 08:13:10

您拥有的几个选项是:

  1. &lt; br/&gt;放在字符串中,并设置p.innerhtml = str而不是设置TextContent
let myEl = document.getElementById('myelement');
let data = 'test 1';
data += '<br/>';
data += 'test 2';

myEl.innerHTML = data;
<div id="myelement"></div>

  1. 在字符串中放置\ n字符,然后在元素的CSS中使用pree
let myEl = document.getElementById('myelement');
let data = 'test 1';
data += '\n';
data += 'test 2';

myEl.textContent = data;
#myelement {
  white-space: pre;
}
<div id="myelement"></div>

A couple options you have are:

  1. put a <br/> in the string and set the p.innerHTML = str instead of setting textContent

let myEl = document.getElementById('myelement');
let data = 'test 1';
data += '<br/>';
data += 'test 2';

myEl.innerHTML = data;
<div id="myelement"></div>

OR

  1. put a \n character in the string and then use a white-space: pre in the CSS of your element

let myEl = document.getElementById('myelement');
let data = 'test 1';
data += '\n';
data += 'test 2';

myEl.textContent = data;
#myelement {
  white-space: pre;
}
<div id="myelement"></div>

成熟稳重的好男人 2025-02-03 08:13:10

如果您需要一个可以以文件显示/下载并同时显示在HTML中的字符串,我将使用\ ninnertext

var str;
str+="text" + data[0];
  str+= '\n';
str+="text" + data[1];

var p=document.getElementById("paragraph");
p.innerText = str;


\ \ 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 and innerText :

var str;
str+="text" + data[0];
  str+= '\n';
str+="text" + data[1];

var p=document.getElementById("paragraph");
p.innerText = str;


the \n will be replaced by <br/> automatically when using innerText, and you wont need to style it with whitespace, and you could use the resulting string, to perhaps start a file download

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文