文档:append() 方法
Document.append() 方法将在文档的最后一个子节点之后插入一组 Node 对象或字符串。字符串将被插入为等效的 Text 节点。
此方法将一个子节点追加到 Document。要追加到树中的任意元素,请参阅 Element.append()。
语法
js
append(param1)
append(param1, param2)
append(param1, param2, /* …, */ paramN)
参数
param1, …,paramN-
要插入的一组
Node对象或字符串。
返回值
无(undefined)。
异常
HierarchyRequestErrorDOMException-
在节点无法插入到层次结构中的指定位置时抛出。
示例
将根元素追加到文档
如果您尝试将一个元素追加到一个现有的 HTML 文档中,并且该文档中已存在一个 <html> 元素,则可能会抛出 HierarchyRequestError DOMException。
js
let html = document.createElement("html");
document.append(html);
// HierarchyRequestError: The operation would yield an incorrect node tree.
如果您正在创建一个没有任何现有元素的新文档,则可以追加一个根 HTML 元素(或一个根 SVG 元素)。
js
let doc = new Document();
let html = document.createElement("html");
doc.append(html);
doc.children; // HTMLCollection [<html>]
规范
| 规范 |
|---|
| DOM # ref-for-dom-parentnode-append① |
浏览器兼容性
加载中…