DocumentFragment: prepend() 方法
DocumentFragment.prepend() 方法在文件片段的第一個子節點之前插入一組 Node 物件或字串。字串將被插入為等效的 Text 節點。
此方法將子節點新增到 DocumentFragment 的前面。要將子節點新增到樹中的任意元素,請參閱 Element.prepend()。
語法
js
prepend(param1)
prepend(param1, param2)
prepend(param1, param2, /* …, */ paramN)
引數
param1, …,paramN-
要插入的一組
Node物件或字串。
返回值
無(undefined)。
異常
HierarchyRequestErrorDOMException-
在節點無法插入到層次結構中的指定位置時丟擲。
示例
將元素新增到文件片段
js
let fragment = new DocumentFragment();
let div = document.createElement("div");
let p = document.createElement("p");
fragment.append(p);
fragment.prepend(div);
fragment.children; // HTMLCollection [<div>, <p>]
規範
| 規範 |
|---|
| DOM # ref-for-dom-parentnode-prepend① |
瀏覽器相容性
載入中…