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.
语法
js
after(node1)
after(node1, node2)
after(node1, node2, /* … ,*/ nodeN)
参数
node1
, …,nodeN
-
一组准备插入的
Node
或字符串。
异常
HierarchyRequestError
: 在某些不正确的层级结构进行了插入操作。
示例
插入元素
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