Element:append() 方法
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2018年4月.
Element.append() 方法在当前 Element 的最后一个子节点之后插入一组 Node 对象或字符串对象。被插入的字符串对象等价为 Text 节点。
其与 Node.appendChild() 的差异:
语法
js
append(param1)
append(param1, param2)
append(param1, param2, /* …, */ paramN)
参数
param1、…、paramN-
一组要插入的
Node或字符串对象。
返回值
无(undefined)。
异常
HierarchyRequestErrorDOMException-
当层次结构中的指定位置不能插入节点时抛出该异常。
示例
>附加元素
js
let div = document.createElement("div");
let p = document.createElement("p");
div.append(p);
console.log(div.childNodes); // NodeList [ <p> ]
附加文本
js
let div = document.createElement("div");
div.append("一些文本");
console.log(div.textContent); // "一些文本"
附加元素和文本
js
let div = document.createElement("div");
let p = document.createElement("p");
div.append("一些文本", p);
console.log(div.childNodes); // NodeList [ #text "一些文本", <p> ]
append 是不可绑定作用域方法
append() 方法无法被绑定到 with 语句的作用域中。参见 Symbol.unscopables 以了解更多信息。
js
let div = document.createElement("div");
with (div) {
append("foo");
}
// ReferenceError: append is not defined
规范
| Specification |
|---|
| DOM> # ref-for-dom-parentnode-append①> |
浏览器兼容性
Loading…