HTML DOM createDocumentFragment()Method

Document 对象参考手册Document Object

Example

Create a documentFragment node:

var d=document.createDocumentFragment();
d.appendChild(document.getElementsByTagName("LI")[0]);
d.childNodes[0].childNodes[0].nodeValue="Milk";
document.getElementsByTagName("UL")[0].appendChild(d);

Output result:

Before modification:

  • Coffee
  • Tea

After modification:

  • Tea
  • Milk

Try it yourself »

Definition and Usage

The createDocumentFragment() method creates a virtual node object, which contains all properties and methods.

When you want to extract a part of a document, change, add, or delete some content and insert it at the end of the document, you can use the createDocumentFragment() method.

You can also use the document object to perform these changes, but to prevent the file structure from being damaged, the createDocumentFragment() method can change the structure and nodes of the document more safely.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the createDocumentFragment() method


Syntax

document.createDocumentFragment()

Parameters

None.

Return Value

Type Description
DocumentFragment Object Creates a document fragment object

Technical Details

DOM Version Core Level 1 Document Object


Document 对象参考手册Document Object

Other Extensions