简体   繁体   English

如何在此 JavaScript.text 行中开始新行?

[英]How do I start a new line in this JavaScript .text line?

So I'm new to javascript and I'm exploring it by developing a simple clock on a FitBit.所以我是 javascript 的新手,我正在通过在 FitBit 上开发一个简单的时钟来探索它。 Here's my code:这是我的代码:

if (hours < 12){
    myLabel.text = `${hours}:${mins}:${secs}:${"AM"}:${"\n"}:${"Sunday"}`;
  }

I want it to print the following:我希望它打印以下内容:

Hours, Minutes, Seconds
Sunday

However, it's printing everything on the same line:但是,它在同一行打印所有内容:

Hours, Minutes, Seconds, Sunday 

I've tried \n but that didn't work.我试过 \n 但这没有用。 Thanks!谢谢!

Firstly, `${"\n"}` is equivelent to `\n` .首先, `${"\n"}`等价于`\n` Likewise AM and Sunday unless you make them dynamic in the future. AMSunday也是如此,除非你让它们在未来充满活力。

Secondly, \n should be used to line break JS strings, not HTML text.其次, \n应该用于换行 JS 字符串,而不是 HTML 文本。

Eg,例如,

let str = 'line 1 \n line 2`; // this is fine

element.textContent = 'line 1 \n line2'; // this is bad

<element>line 1 \n line 2</element> // (HTML) this is bad

The reason is because most (see HTML pre and CSS white-space for exceptions) HTML elemnts will be rendered with disregard to superflous white space and new lines.原因是因为大多数(见 HTML pre和 CSS white-space for exceptions)HTML 元素将被渲染而无视多余的空白和新行。

Instead, you should use seperate elements if you want to explicitly present a new line in HTML.相反,如果要在 HTML 中显式显示新行,则应使用单独的元素。 Eg例如

<p>line 1</p>
<p>line 2</p>

In your case,在你的情况下,

 let hours = 3, mins = 5, secs = 7; if (hours < 12) { document.querySelector('#line1').textContent = `${hours}:${mins}:${secs}:AM`; document.querySelector('#line2').textContent = 'Sunday'; }
 <label> <div id="line1"></div> <div id="line2"></div> </label>

You need to use a element for multiline.您需要为多行使用一个元素。 Then from JavaScript, you use \n for a line break, or just an actual line break in the SVG.然后从 JavaScript 开始,您使用\n作为换行符,或者只是 SVG 中的实际换行符。

Ref:https://twitter.com/fitbitdev/status/930945957151178752?lang=en参考:https://twitter.com/fitbitdev/status/930945957151178752?lang=en

在此处输入图像描述

Sol: https://dev.fitbit.com/build/guides/user-interface/svg/溶胶: https://dev.fitbit.com/build/guides/user-interface/svg/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM