如何在模板字符串中放置换行符

时间:2019-03-15 04:58:16

标签: javascript

我有一个模板字符串,其中我正在使用<td> <button class="btn btn-sm">ok</button><button class="btn btn-sm">ko</button> </td> ,如下所示:

array

不显示换行符。在这种情况下如何放置换行符?

2 个答案:

答案 0 :(得分:0)

如果您想在控制台中显示它,它会完美工作:

var arr = [1, 2, 3];
var templateString = `array is ${arr.join(",\n")}`;
console.log(templateString);

如果要以HTML形式显示,请使用HTML换行符(<br>):

var arr = [1, 2, 3];
var templateString = `array is ${arr.join(",<br />")}`;
document.write(templateString);

答案 1 :(得分:0)

您可以这样做:

var arr = ["Saab", "Volvo", "BMW", "dd"];
var templateString = `array is ${arr.join(",<br>")}`;
//This is getting displayed in HTML hence used break line so that it renders.
document.getElementById("demo").innerHTML = templateString;