zl程序教程

您现在的位置是:首页 >  前端

当前栏目

[HTML 5] Inserting DOM element

HTML dom Element
2023-09-14 09:00:47 时间
const app = document.getElementById('app');
app.innerHTML = `
  <h1>JavaScript DOM</h1>
`;

const div = document.createElement('div');
const span = document.createElement('span');
const p = document.createElement('p');
const i = document.createElement('i');
const b = document.createElement('b');

div.append(span);
div.prepend(p);
// p.before(i);
p.after(i);

// Before: old way using insertBefore
// i.parentNode.insertBefore(b, i);

// After: old way using insertBefore + nextSibling
i.parentNode.insertBefore(b, i.nextSibling);

console.log(div);