2-ES6之模板字符串

 模板字符串
1)模板字符串相当于加强版的字符串,用反引号 `,除了作为普通字符串,还可以用来定义多行字符串,还可以在字符串中加入变量和表达式。
2)变量名写在 ${} 中,${} 中可以放入 JavaScript 表达式。
3)模板字符串中的换行和空格都是会被保留的

<body>
    <div class="box">
    </div>
    <script>
        const oBox =document.querySelector('.box');
        let id=1;
        let name ="111";
        let htmlStr = ` <ul>
            <li>
                <p id="${id}">${name}</p>
            </li>
        </ul> `
        oBox.innerHTML =htmlStr;

        
    </script>
</body>

  效果

<div class="box"> <ul>
            <li>
                <p id="1">111</p>
            </li>
        </ul> </div>

  

原文地址:https://www.cnblogs.com/lixiang1013/p/13588265.html