14.javascript 字符串插值

1.当要在一个字符串中即写双引号,又写单引号,而且还多行的时候,我们可以使用反引号实现``

let text =
`The quick
"brown fox"
'jumps over'
the lazy dog`;
//结果:The quick "brown fox" 'jumps over' the lazy dog

2. 插值即我们使用使用${}模式在大括号里面写变量名称,来插入变量名称对应的值,同时也可在大括号中对变量名进行简单运算

 有个高大上的名字,叫做:模板字面量

let price = 10;
let VAT = 0.25;

let total = `Total: ${(price * (1 + VAT)).toFixed(2)}`;
//结果:Total: 12.50
let header = "Templates Literals";
let tags = ["template literals", "javascript", "es6"];

let html = `<h2>${header}</h2><ul>`;

for (const x of tags) {
  html += `<li>${x}</li>`;
}

html += `</ul>`;
document.getElementById("demo").innerHTML = html;

  

  

posted @ 2022-01-06 00:08  种太阳  阅读(503)  评论(0编辑  收藏  举报