3.模板字符串

模板字符串

  用反引号 `,变量用${}

const oBox = document.querySelector('.box');
   let id = 1,
       name = '小马哥';
   let htmlStr = `<ul>
       <li>
          <p id=${id}>${name}</p>
        </li>
      </ul>`;
        oBox.innerHTML = htmlStr;

渲染结果

<div class="box">

  <ul>
    <li>
      <p id="1">小马哥</p>
    </li>
  </ul>

</div>

字符串中调用函数

function f(){
  return "have fun!";
}
let string2= `Game start,${f()}`;
console.log(string2);  // Game start,have fun!
原文地址:https://www.cnblogs.com/sunny666/p/12983914.html