从ES6到HTML的多行模板字符串不包括换行符

Multiline template strings from ES6 into HTML not including line breaks

本文关键字:字符串 不包括 换行符 ES6 HTML      更新时间:2023-09-26

我在JS文件中有一个很大的模板文本(使用ES6标准),看起来类似于:

let stringLiteral = `this is a large
                     string and it also 
                     has many lines in it
                     and this is a larger line break`

为了把它放在我的HTML文件中,我只使用:

document.getElementById('content').innerHTML = stringLiteral;

但是,当我这样做的时候,换行符并没有包含在HTML中。我该怎么做才能做到这一点?

默认情况下,文本节点中的空白区域被折叠。使用<br /><br>作为换行符,或将white-space: pre;添加到该元素的css中。