DocumentFragment

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.

A interface do DocumentFragment representa um objeto de documento mínimo que não possui pai. Ela é utilizada como uma versão leve de Document para armazenar fragmentos bem formados ou fragments potencialmente mal formados de XML.

Vários outros métodos podem usar um document fragment como argumento (ex. qualquer interface de Node como Node.appendChild e Node.insertBefore) em casos em que os filhos do fragment são acrescentados ou inseridos, e não o próprio fragment.

Essa interface também é excelente para ser usada com Web components: elementos <template> contém um DocumentFragment na propriedade HTMLTemplateElement.content deles.

Um DocumentFragment pode ser criado usando o método document.createDocumentFragment ou o construtor.

Propriedades

Essa interface não tem uma propriedade específica, mas herda de seu pai, Node, e implementa aquelas da interface ParentNode.

ParentNode.children Somente leitura Experimental

Returns a live HTMLCollection containing all objects of type Node that are children of the DocumentFragment object.

ParentNode.firstElementChild Somente leitura Experimental

Returns the Element that is the first child of the DocumentFragment object, or null if there is none.

ParentNode.lastElementChild Somente leitura Experimental

Returns the Element that is the last child of the DocumentFragment object, or null if there is none.

ParentNode.childElementCount Somente leitura Experimental

Returns an unsigned long giving the amount of children that the DocumentFragment has.

Construtor

DocumentFragment() Experimental

Retorna um objeto DocumentFragment vazio.

Métodos

This interface inherits the methods of its parent, Node, and implements those of the ParentNode interface.

DocumentFragment.find() Experimental

Returns the first matching Element in the tree of the DocumentFragment.

DocumentFragment.findAll() Experimental

Returns a NodeList of matching Element in the tree of the DocumentFragment.

DocumentFragment.querySelector()

Returns the first Element node within the DocumentFragment, in document order, that matches the specified selectors.

DocumentFragment.querySelectorAll()

Returns a NodeList of all the Element nodes within the DocumentFragment that match the specified selectors.

DocumentFragment.getElementById()

Returns the first Element node within the DocumentFragment, in document order, that matches the specified ID.

Especificações

Specification
DOM
# interface-documentfragment

Compatibilidade com navegadores

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
DocumentFragment
DocumentFragment() constructor
append
childElementCount
children
firstElementChild
getElementById
lastElementChild
moveBefore
Experimental
prepend
querySelector
querySelectorAll
replaceChildren

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
In development. Supported in a pre-release version.
In development. Supported in a pre-release version.
No support
No support
Experimental. Expect behavior to change in the future.

Veja também