Document.createDocumentFragment()
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.
io error: No such file or directory (os error 2) (/home/runner/work/yari/yari/mdn/translated-content/files/zh-tw/web/api/document/index.md)
建立新的 DocumentFragment
.
語法
var fragment = document.createDocumentFragment();
fragment
是 DocumentFragment
的一個參考物件。
描述
舉例
這個例子中用清單來呈現主流瀏覽器。
HTML
html
<ul id="ul"></ul>
JavaScript
js
var element = document.getElementById("ul"); // assuming ul exists
var fragment = document.createDocumentFragment();
var browsers = ["Firefox", "Chrome", "Opera", "Safari", "Internet Explorer"];
browsers.forEach(function (browser) {
var li = document.createElement("li");
li.textContent = browser;
fragment.appendChild(li);
});
element.appendChild(fragment);
結果
在 jsfiddle 上看範例結果。
規格
Specification |
---|
DOM Standard # ref-for-dom-document-createdocumentfragment① |
瀏覽器相容性
BCD tables only load in the browser