About javascript document. createDocumentFragment ()

Source: Internet
Author: User

He supports the following DOM2 methods:
AppendChild, cloneNode, hasAttributes, hasChildNodes, insertBefore, normalize, removeChild, replaceChild.
The following DOM2 features are also supported:
Attributes, childNodes, firstChild, lastChild, localName, namespaceURI, nextSibling, nodeName, nodeType, nodeValue, ownerDocument, parentNode, prefix, previussibling, textContent.
For other methods, you can set documentFragment as a parameter (such as the appendChild and insertBefore methods of Node). In this way, fragment can be appended to the parent object.
Example: Copy codeThe Code is as follows: var frag = document. createDocumentFragment ();
Frag. appendChild (document. createTextNode ('ipsum Lorem '));
Document. body. appendChild (frag );

Document. createDocumentFragment () is to save on DOM usage. Every JavaScript operation on DOM changes the realization of the page and refresh the whole page, which consumes a lot of time. To solve this problem, you can create a File Fragment, attach all the new nodes to it, and add the content of the file fragment to the document at one time.Copy codeThe Code is as follows: var oui = document. getElementById ("oItem ");
For (var I = 0; I <10; I ++)
{
Var oli = document. createElement ("li ");
Oui. appendChild (oli );
Oli. appendChild (document. createTextNode ("Item" + I ));
}

The above code calls oui. appendChild (oli) in a loop. Every time this statement is executed, the browser updates the page. Next, the text node is added to oui. appendChild () and the page must be updated. Therefore, we need to update the page 20 times in total.
To optimize the page, we need to minimize DOM operations, add the list item after adding a text node, and use creatDocumentFragment () Reasonably. The Code is as follows:Copy codeThe Code is as follows: var oui = document. getElementById ("oItem ");
Var oFragment = document. createDocumentFragment ();
For (var I = 0; I <10; I ++ ){
Var oli = document. createElement ("li ");
Oli. appendChild (document. createTextNode ("Item" + I ));
OFragment. appendChild (oli );
}
Oui. appendChild (oFragment );

W3C reference: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-B63ED1A3
-------------------------------------------
DocumentFragment is a "lightweight" or "minimal" Document object. it is very common to want to be able to extract a portion of a document's tree or to create a new fragment of a document. imagine implementing a user command like cut or rearranging a document by moving fragments around. it is desirable to have an object which can hold such fragments and it is quite natural to use a Node for this purpose. while it is true that a Document object cocould fulfill this role, a Document object can potentially be a heavyweight object, depending on the underlying implementation. what is really needed for this is a very lightweight object. documentFragment is such an object.

Furthermore, varous operations -- such as inserting nodes as children of another Node -- may take DocumentFragment objects as arguments; this results in all the child nodes of the DocumentFragment being moved to the child list of this node.

The children of a DocumentFragment node are zero or more nodes representing the tops of any sub-trees defining the structure of the document. documentFragment nodes do not need to be well-formed XML documents (although they do need to follow the rules imposed upon well-formed XML parsed entities, which can have multiple top nodes ). for example, a DocumentFragment might have only one child and that child node cocould be a Text node. such a structure model represents neither an HTML document nor a well-formed XML document.

When a DocumentFragment is inserted into a Document (or indeed any other Node that may take children) the children of the DocumentFragment and not the DocumentFragment itself are inserted into the Node. this makes the DocumentFragment very useful when the user wishes to create nodes that are siblings; the DocumentFragment acts as the parent of these nodes so that the user can use the standard methods from the Node interface, such as insertBefore and appendChild.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.