2.模板字符串

213 阅读1分钟

模板字符串:使用tab键上面的反引号``,插入变量时使用${变量名}

<div class="box">
</div>
<script>
    const oBox = document.querySelector('.box');
    let id = 1,name = 'tom';
    //原js写法,字符串拼接
    oBox.innerHTML = "<ul><li><p id=" + id + ">" + name + "</p></li></ul>";
    //es6写法,模板字符串
    let htmlStr = `<ul>
        <li>
            <p id=${id}>${name}</p>
        </li>
    </ul>`;
    oBox.innerHTML = htmlStr;
</script>