[HTML 5] Inserting DOM as String template insertAdjacentHTML

const app = document.getElementById('app');
app.innerHTML = `
  <h1>JavaScript DOM</h1>
  <ul>
    <li>1</li>
  </ul>
`;

const ul = app.querySelector('ul');

ul.insertAdjacentHTML('beforebegin', '<p>Before</p>'); // <p>Before</p><ul><li>1</li></ul>
ul.insertAdjacentHTML('afterbegin', '<li>First</li>'); // <li>First</li><ul><li>1</li></ul>
ul.insertAdjacentHTML('beforeend', '<li>Last</li>'); // <ul><li>1</li><li>Last</li></ul>
ul.insertAdjacentHTML('afterend', '<p>After</p>'); // <ul><li>1</li></ul><p>After</p>

 Using '

insertAdjacentHTML

' is faster than innerHTML.

上一篇:JS实现倒计时


下一篇:渲染HTML的方式