问题
I'm trying to use a template string to update infoDiv's text and for some reason all this text is in one line. What am I doing wrong? None of these solutions work - they're just rendered.
infoDiv.textContent = `Hi!
It's a ${counter} question quiz.
It takes about ${time_minutes} minutes to solve it.
Are you ready?`
EDIT:
I found a CSS solution here (white-space: pre-line;
).
let counter = 8;
let time_minutes = 3;
infoDiv.textContent = `Hi!
It's a ${counter} question quiz.
It takes about ${time_minutes} minutes to solve it.
Are you ready?`
<div id="infoDiv" style="white-space: pre-line"></div>
回答1:
Style the div with white-space
CSS property.
const counter = 22;
const time_minutes = 60;
infoDiv.textContent = `Hi!
It's a ${counter} question quiz.
It takes about ${time_minutes} minutes to solve it.
Are you ready?`
<div id="infoDiv" style="white-space: pre-wrap"></div>
来源:https://stackoverflow.com/questions/43729577/string-template-new-line-doesnt-show