ES6语法之 template string

有睿
2023-12-01

当我们要插入大段的html内容到文档中时,传统的写法非常麻烦,

$("#result").append(
  "There are <b>" + basket.count + "</b> " +
  "items in your basket, " + "<em>" + basket.onSale + "</em> are on sale!" );

使用大量的+ 号和字符串拼接 

而使用ES6的新特性模板字符串``后,可以这样写

$("#result").append(`
  There are <b>${basket.count}</b> items
   in your basket, <em>${basket.onSale}</em> are on sale! `);

`aa${变量名}aaa` 用${}这种方式来引用变量

转载于:https://www.cnblogs.com/studyhtml5/p/7150760.html

 类似资料: