sunny-cheng  

模板字符串

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

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!

 

posted on 2020-05-28 21:06  sunny-cheng  阅读(198)  评论(0编辑  收藏  举报