【发布时间】:2019-01-19 14:29:25
【问题描述】:
我有一个脚本,在按钮单击时显示随机句子。 我想在某些句子中添加换行符。示例:
“这是第一句话”
变成:
“这是
第一句话”
我尝试过使用\n 和<br>,但都没有。
我还能尝试什么?
const p = document.getElementById("sentences");
const origSentences = ["This is the first sentence.", "This is the second sentence.", "This is the third sentence."];
let remainingSentences = [];
function randomSentence() {
if (remainingSentences.length === 0) remainingSentences = origSentences.slice();
const {
length
} = remainingSentences;
const [quote] = remainingSentences.splice(Math.floor(Math.random() * length), 1);
p.textContent = quote;
}
<p><button onclick="randomSentence()" type="button">Random Sentence</button></p>
<p id="sentences"></p>
【问题讨论】:
标签: javascript arrays string line-breaks