04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 10: The Document Object Model<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

nodeType is 11.<br />

nodeName is “ #document - fragment ” .<br />

nodeValue is null .<br />

parentNode is null .<br />

Child nodes may be Element , ProcessingInstruction , Comment , Text , CDATASection , or<br />

EntityReference .<br />

A document fragment cannot be added to a document directly. Instead, it acts as a repository for other<br />

nodes that may need to be added to the document. Document fragments are created using the<br />

document.createDocumentFragment() method, shown here:<br />

var fragment = document.createDocumentFragment();<br />

Document fragments inherit all methods from Node and are typically used to perform DOM<br />

manipulations that are to be applied to a document. If a node from the document is added to a document<br />

fragment, that node is removed from the document tree and won ’ t be rendered by the browser. New<br />

nodes that are added to a document fragment are also not part of the document tree. The contents of<br />

a document fragment can be added to a document via appendChild() or insertBefore() . When a<br />

document fragment is passed in as an argument to either of these methods, all of the document<br />

fragment ’ s child nodes are added in that spot; the document fragment itself is never added to the<br />

document tree. For example, consider the following HTML:<br />

< ul id=”myList” > < /ul ><br />

Suppose you would like to add three list items to this < ul > element. Adding each item directly to the<br />

element causes the browser to rerender the page with the new information. To avoid this, the following<br />

code example uses a document fragment to create the list items and then add them all at the same time:<br />

var fragment = document.createDocumentFragment();<br />

var ul = document.getElementById(“myList”);<br />

var li = null;<br />

for (var i=0; i < 3; i++){<br />

li = document.createElement(“li”);<br />

li.appendChild(document.createTextNode(“Item “ + (i+1)));<br />

fragment.appendChild(li);<br />

}<br />

ul.appendChild(fragment);<br />

This example begins by creating a document fragment and retrieving a reference to the < ul > element.<br />

The for loop creates three list items, each with text indicating which item they are. To do this, an < li ><br />

element is created and then a text node is created and added to that element. The < li > element is<br />

then added to the document fragment using appendChild() . When the loop is complete, all of the<br />

items are added to the < ul > element by calling appendChild() and passing in the document fragment.<br />

At that point, the document fragment ’ s child nodes are all removed and placed onto the < ul > element.<br />

295

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!