1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
---
title: Document.createDocumentFragment()
slug: Web/API/Document/createDocumentFragment
tags:
- API
- DOM
- Document
- DocumentFragment
- Method
- Reference
translation_of: Web/API/Document/createDocumentFragment
---
<div>{{ApiRef("DOM")}}</div>
<p>Erzeugt ein neues {{domxref("DocumentFragment")}} Objekt.</p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox">var <var>fragment</var> = document.createDocumentFragment();
</pre>
<p><code>fragment</code> ist hierbei eine Referenz zu einem neu erstellten, leeren {{domxref("DocumentFragment")}} Objekt.</p>
<h2 id="Beschreibung">Beschreibung</h2>
<p><code>DocumentFragment</code>s sind DOM Knoten (DOM Nodes). Sie sind nicht Teil des Haupt- oder Seiten-DOM-Baums. Üblicherweise werden sie verwendet, um einen Teilbaum mit Objekten und Unterobjekten zu erstellen und das Ergebnis anschließend in den Seiten-DOM-Baum einzufügen. In dem DOM-Baum wird das document fragment dann ersetzt mit allen Kindelementen.</p>
<p>Da das gesamte DocumentFragment <strong>nur im Speicher</strong> vorliegt ("in memory"<strong>)</strong> und nicht Teil des Seiten-DOM-Baums ist, führen Veränderungen in dem DocumentFragment, wie etwa das Hinzufügen von Elementen, nicht zu einem page <a href="https://developers.google.com/speed/articles/reflow?csw=1">reflow</a> (die Berechnung der Element Positionen und Geometrie). Dementsprechend führt die Nutzung von DocumentFragments zu einer <a href="http://ejohn.org/blog/dom-documentfragments/">besseren Performance</a>.</p>
<h2 id="Beispiel">Beispiel</h2>
<p>Dieses Beispiel erzeugt eine Liste gängiger Browser.</p>
<h3 id="HTML">HTML</h3>
<pre class="brush: html"><ul id="ul">
</ul></pre>
<h3 id="JavaScript">JavaScript</h3>
<pre class="brush: 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);
</pre>
<h3 id="Resultat">Resultat</h3>
<p>{{EmbedLiveSample("Example", 600, 140)}}</p>
<h2 id="Spezifikationen">Spezifikationen</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Anmerkungen</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('DOM WHATWG', '#dom-document-createdocumentfragment', 'Document.createDocumentFragment()')}}</td>
<td>{{Spec2('DOM WHATWG')}}</td>
<td>Initiale Definition in der DOM 1 Spezifikation</td>
</tr>
</tbody>
</table>
<h2 id="Browserkompatibilität">Browserkompatibilität</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Firefox (Gecko)</th>
<th>Chrome</th>
<th>Edge</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Firefox Mobile (Gecko)</th>
<th>Android</th>
<th>Edge</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<h2 id="See_also">See also</h2>
<ul>
<li>{{domxref("DOMImplementation.createDocument", "document.implementation.createDocument()")}}</li>
<li>{{domxref("documentFragment")}}</li>
</ul>
|