1. 插入某个节点之后, 使用: Node.prototype.insertBefore(parentNode, target);

 

var p = document.createElement('p');
document.body.insertBefore(p, document.body.firstChild);

 

2. 插入某个节点之前, 需要结合 Node.prototype.nextSibling; 

var p = document.createElement('p');
document.body.insertBefore(p, targetNode.nextSibling); 

 

注意, 如果第二个参数为 null 或空, 则会自动插入到父节点内子节点的最后一位.

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-15
  • 2022-12-23
猜你喜欢
  • 2021-05-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-09
  • 2022-12-23
相关资源
相似解决方案