Element.after()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Element.after() 方法会在其父节点的子节点列表中插入一些 NodeDOMString 对象。插入位置为该节点之后。DOMString 对象会被以 Text 的形式插入。

语法

js
after(node1)
after(node1, node2)
after(node1, node2, /* … ,*/ nodeN)

参数

node1, …, nodeN

一组准备插入的 Node 或字符串。

异常

示例

插入元素

js
let container = document.createElement("div");
let p = document.createElement("p");
container.appendChild(p);
let span = document.createElement("span");

p.after(span);

console.log(container.outerHTML);
// "<div><p></p><span></span></div>"

插入文本

js
let container = document.createElement("div");
let p = document.createElement("p");
container.appendChild(p);

p.after("Text");

console.log(container.outerHTML);
// "<div><p></p>Text</div>"

同时插入元素和文本

js
let container = document.createElement("div");
let p = document.createElement("p");
container.appendChild(p);
let span = document.createElement("span");

p.after(span, "Text");

console.log(container.outerHTML);
// "<div><p></p><span></span>Text</div>"

规范

Specification
DOM Standard
# ref-for-dom-childnode-after①

浏览器兼容性

BCD tables only load in the browser

参见