Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1089341 of DocumentFragment

  • Revision slug: Web/API/DocumentFragment
  • Revision title: DocumentFragment
  • Revision id: 1089341
  • Created:
  • Creator: stevemao
  • Is current revision? No
  • Comment

Revision Content

{{ ApiRef("DOM") }}

The DocumentFragment interface represents a minimal document object that has no parent. It is used as a light-weight version of {{domxref("Document")}} to store well-formed or potentially non-well-formed fragments of XML.

Various other methods can take a document fragment as an argument (e.g., any {{domxref("Node")}} interface methods such as {{domxref("Node.appendChild")}} and {{domxref("Node.insertBefore")}}), in which case the children of the fragment are appended or inserted, not the fragment itself. The fragment itself continues to exist (in memory) but now has no children.

This interface is also of great use with Web components: {{HTMLElement("template")}} elements contain a DocumentFragment in their {{domxref("HTMLTemplateElement.content")}} property.

An empty DocumentFragment can be created using the {{domxref("document.createDocumentFragment")}} method or the constructor.

Properties

This interface has no specific property, but inherits those of its parent, {{domxref("Node")}}, and implements those of the {{domxref("ParentNode")}} interface.

{{ domxref("ParentNode.children") }} {{readonlyInline}}{{experimental_inline}}
Returns a live {{domxref("HTMLCollection")}} containing all objects of type {{domxref("Element")}} that are children of the DocumentFragment object.
{{ domxref("ParentNode.firstElementChild") }} {{readonlyInline}}{{experimental_inline}}
Returns the {{domxref("Element")}} that is the first child of the DocumentFragment object, or null if there is none.
{{ domxref("ParentNode.lastElementChild") }} {{readonlyInline}}{{experimental_inline}}
Returns the {{domxref("Element")}} that is the last child of the DocumentFragment object, or null if there is none.
{{ domxref("ParentNode.childElementCount") }} {{readonlyInline}}{{experimental_inline}}
Returns an unsigned long giving the amount of children that the DocumentFragment has.

Constructor

{{ domxref("DocumentFragment.DocumentFragment()", "DocumentFragment()") }} {{experimental_inline}}
Returns an empty DocumentFragment object.

Methods

This interface inherits the methods of its parent, {{domxref("Node")}}, and implements those of the {{domxref("ParentNode")}} interface.

{{domxref("DocumentFragment.find()")}} {{experimental_inline}}
Returns the first matching {{domxref("Element")}} in the tree of the DocumentFragment.
{{domxref("DocumentFragment.findAll()")}} {{experimental_inline}}
Returns a {{domxref("NodeList")}} of matching {{domxref("Element")}} in the tree of the DocumentFragment.
{{domxref("DocumentFragment.querySelector()")}}
Returns the first {{domxref("Element")}} node within the DocumentFragment, in document order, that matches the specified selectors.
{{domxref("DocumentFragment.querySelectorAll()")}}
Returns a {{domxref("NodeList")}} of all the {{domxref("Element")}} nodes within the DocumentFragment that match the specified selectors.
{{domxref("DocumentFragment.getElementById()")}}
Returns the first {{domxref("Element")}} node within the DocumentFragment, in document order, that matches the specified ID.

Specifications

Specification Status Comment
{{SpecName('DOM WHATWG', '#interface-documentfragment', 'DocumentFragment')}} {{Spec2('DOM WHATWG')}} Added the constructor and the implementation of {{domxref("ParentNode")}}.
{{SpecName('Selectors API Level 2', '#the-apis', 'DocumentFragment')}} {{Spec2('Selectors API Level 2')}} Added the find() and findAll() methods.
{{SpecName('Selectors API Level 1', '#the-apis', 'DocumentFragment')}} {{Spec2('Selectors API Level 1')}} Added the querySelector() and querySelectorAll() methods.
{{SpecName('DOM3 Core', 'core.html#ID-B63ED1A3', 'DocumentFragment')}} {{Spec2('DOM3 Core')}} No change from {{SpecName('DOM2 Core')}}
{{SpecName('DOM2 Core', 'core.html#ID-B63ED1A3', 'DocumentFragment')}} {{Spec2('DOM2 Core')}} No change from {{SpecName('DOM1')}}
{{SpecName('DOM1', 'level-one-core.html#ID-B63ED1A3', 'DocumentFragment')}} {{Spec2('DOM1')}} Initial definition

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0 {{CompatGeckoDesktop("1.0")}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
querySelector() and querySelectorAll() 1.0 {{CompatGeckoDesktop("1.9.1")}} 8.0 10.0 3.2 (525.3)
findAll() and find() {{experimental_inline}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
DocumentFragment() constructor {{experimental_inline}} 28.0 {{CompatGeckoDesktop("24.0")}} {{CompatNo}} 15.0 {{CompatNo}}
ParentNode properties {{experimental_inline}} 28.0 {{CompatGeckoDesktop("25.0")}} {{CompatNo}} 15.0 {{CompatNo}}
ParentNode methods {{experimental_inline}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatGeckoMobile("1.0")}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
querySelector() and querySelectorAll() 2.1 {{CompatGeckoMobile("1.0")}} 8.0 10.0 3.2 (525.3)
findAll() and find() {{experimental_inline}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
DocumentFragment() constructor {{experimental_inline}} {{CompatUnknown}} {{CompatGeckoMobile("24.0")}} {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}}
ParentNode properties {{experimental_inline}} {{CompatVersionUnknown}} {{CompatGeckoMobile("25.0")}} {{CompatNo}} 5.0 {{CompatNo}}
ParentNode methods {{experimental_inline}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

See also

Revision Source

<p>{{ ApiRef("DOM") }}</p>

<p>The <strong><span class="external"><code>DocumentFragment</code></span></strong> interface represents a minimal document object that has no parent. It is used as a light-weight version of {{domxref("Document")}} to store well-formed or potentially non-well-formed fragments of XML.</p>

<p>Various other methods can take a document fragment as an argument (e.g., any {{domxref("Node")}} interface methods such as {{domxref("Node.appendChild")}} and {{domxref("Node.insertBefore")}}), in which case the children of the fragment are appended or inserted, not the fragment itself. The fragment itself continues to exist (in memory) but now has no children.</p>

<p>This interface is also of great use with Web components: {{HTMLElement("template")}} elements contain a <code>DocumentFragment</code> in their {{domxref("HTMLTemplateElement.content")}} property.</p>

<p>An empty <code>DocumentFragment</code> can be created using the {{domxref("document.createDocumentFragment")}} method or the constructor.</p>

<h2 id="Specification" name="Specification">Properties</h2>

<p><em>This interface has no specific property, but inherits those of its parent, </em><em>{{domxref("Node")}}, and implements those of the {{domxref("ParentNode")}} interface.</em></p>

<dl>
 <dt>{{ domxref("ParentNode.children") }} {{readonlyInline}}{{experimental_inline}}</dt>
 <dd>Returns a live {{domxref("HTMLCollection")}} containing all objects of type {{domxref("Element")}} that are children of the <code>DocumentFragment</code> object.</dd>
 <dt>{{ domxref("ParentNode.firstElementChild") }} {{readonlyInline}}{{experimental_inline}}</dt>
 <dd>Returns the {{domxref("Element")}} that is the first child of the <code>DocumentFragment</code> object, or <code>null</code> if there is none.</dd>
 <dt>{{ domxref("ParentNode.lastElementChild") }} {{readonlyInline}}{{experimental_inline}}</dt>
 <dd>Returns the {{domxref("Element")}} that is the last child of the <code>DocumentFragment</code> object, or <code>null</code> if there is none.</dd>
 <dt>{{ domxref("ParentNode.childElementCount") }} {{readonlyInline}}{{experimental_inline}}</dt>
 <dd>Returns an <code>unsigned long</code> giving the amount of children that the <code>DocumentFragment</code> has.</dd>
</dl>

<h2 id="Constructor">Constructor</h2>

<dl>
 <dt>{{ domxref("DocumentFragment.DocumentFragment()", "DocumentFragment()") }} {{experimental_inline}}</dt>
 <dd>Returns an empty <code>DocumentFragment</code> object.</dd>
</dl>

<h2 id="Methods">Methods</h2>

<p><em>This interface inherits the methods of its parent, {{domxref("Node")}}<em>, and implements those of the {{domxref("ParentNode")}} interface</em></em><em>.</em></p>

<dl>
 <dt>{{domxref("DocumentFragment.find()")}} {{experimental_inline}}</dt>
 <dd>Returns the first matching {{domxref("Element")}} in the tree of the <code>DocumentFragment</code>.</dd>
 <dt>{{domxref("DocumentFragment.findAll()")}} {{experimental_inline}}</dt>
 <dd>Returns a {{domxref("NodeList")}} of matching {{domxref("Element")}} in the tree of the <code>DocumentFragment</code>.</dd>
 <dt>{{domxref("DocumentFragment.querySelector()")}}</dt>
 <dd>Returns the first {{domxref("Element")}} node within the <code>DocumentFragment</code>, in document order, that matches the specified selectors.</dd>
 <dt>{{domxref("DocumentFragment.querySelectorAll()")}}</dt>
 <dd>Returns a {{domxref("NodeList")}} of all the {{domxref("Element")}} nodes within the <code>DocumentFragment</code> that match the specified selectors.</dd>
</dl>

<dl>
 <dt>{{domxref("DocumentFragment.getElementById()")}}</dt>
 <dd>Returns the first {{domxref("Element")}} node within the&nbsp;<code style="font-size: 14px;">DocumentFragment</code>, in document order, that matches the specified ID.</dd>
</dl>

<h2 id="Specifications" name="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('DOM WHATWG', '#interface-documentfragment', 'DocumentFragment')}}</td>
   <td>{{Spec2('DOM WHATWG')}}</td>
   <td>Added the constructor and the implementation of {{domxref("ParentNode")}}.</td>
  </tr>
  <tr>
   <td>{{SpecName('Selectors API Level 2', '#the-apis', 'DocumentFragment')}}</td>
   <td>{{Spec2('Selectors API Level 2')}}</td>
   <td>Added the <code>find()</code> and <code>findAll()</code> methods.</td>
  </tr>
  <tr>
   <td>{{SpecName('Selectors API Level 1', '#the-apis', 'DocumentFragment')}}</td>
   <td>{{Spec2('Selectors API Level 1')}}</td>
   <td>Added the <code>querySelector()</code> and <code>querySelectorAll()</code> methods.</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM3 Core', 'core.html#ID-B63ED1A3', 'DocumentFragment')}}</td>
   <td>{{Spec2('DOM3 Core')}}</td>
   <td>No change from {{SpecName('DOM2 Core')}}</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM2 Core', 'core.html#ID-B63ED1A3', 'DocumentFragment')}}</td>
   <td>{{Spec2('DOM2 Core')}}</td>
   <td>No change from {{SpecName('DOM1')}}</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM1', 'level-one-core.html#ID-B63ED1A3', 'DocumentFragment')}}</td>
   <td>{{Spec2('DOM1')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>1.0</td>
   <td>{{CompatGeckoDesktop("1.0")}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td><code>querySelector()</code> and <code>querySelectorAll()</code></td>
   <td>1.0</td>
   <td>{{CompatGeckoDesktop("1.9.1")}}</td>
   <td>8.0</td>
   <td>10.0</td>
   <td>3.2 (525.3)</td>
  </tr>
  <tr>
   <td><code>findAll()</code> and <code>find() </code>{{experimental_inline}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>DocumentFragment()</code> constructor {{experimental_inline}}</td>
   <td>28.0</td>
   <td>{{CompatGeckoDesktop("24.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>15.0</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>ParentNode</code> properties {{experimental_inline}}</td>
   <td>28.0</td>
   <td>{{CompatGeckoDesktop("25.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>15.0</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>ParentNode</code> methods {{experimental_inline}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("1.0")}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td><code>querySelector()</code> and <code>querySelectorAll()</code></td>
   <td>2.1</td>
   <td>{{CompatGeckoMobile("1.0")}}</td>
   <td>8.0</td>
   <td>10.0</td>
   <td>3.2 (525.3)</td>
  </tr>
  <tr>
   <td><code>findAll()</code> and <code>find() </code>{{experimental_inline}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>DocumentFragment()</code> constructor {{experimental_inline}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("24.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>ParentNode</code> properties {{experimental_inline}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("25.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>5.0</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>ParentNode</code> methods {{experimental_inline}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/DOM/DOM_Reference" title="/en-US/docs/DOM/DOM_Reference">The DOM interfaces index.</a></li>
</ul>
Revert to this revision