Java Reference
In-Depth Information
DocumentFragment
The
DocumentFragment
object is a lightweight
Document
object. Its primary purpose is efficiency.
Making many changes to the DOM tree, such as appending several nodes individually, is an expensive
process. It is possible to append
Node
objects to a
DocumentFragment
object, which allows you to
easily and efficiently insert all nodes contained within the
DocumentFragment
into the DOM tree.
The following code shows the use of a
DocumentFragment
:
var documentFragment = document.createDocumentFragment();
for (var i = 0; i < 1000; i++) {
var element = document.createElement(“div”);
var text = document.createTextNode(“Here is test for div #” + i);
element.setAttribute(“id”, i);
documentFragment.appendChild(element);
}
document.body.appendChild(documentFragment);
Without the
DocumentFragment
object, this code would update the DOM tree 1,000 times, thus
degrading performance. With the
DocumentFragment
object, the DOM tree is updated only once.
The
DocumentFragment
object inherits the
Node
object, and as such has
Node
's properties and
methods. It does not have any other properties or methods.
element
Elements are the majority of objects, other than text, that you will encounter in the DOM.
Properties
prOperty NaMe
DesCrIptION
INtrODuCeD
Returns the name of the element. The same as
Node.nodeName
for this node type.
Level 1
tagName
Methods
MethOD NaMe
DesCrIptION
INtrODuCeD
Retrieves the attribute's value by
the specified name.
Level 1
getAttribute(name)
getAttributeNS(namespaceURI,
localName)
Returns the
Attr
object by local
name and namespace URI. Not
for HTML DOMs.
Level 2
continues