How to create a new Document object is independent of the current document

Use: document.implementation . As shown below, Document newly created object can be used normally associated properties and methods, then it's the root of the current document root to make a substitution.

var doc = document.implementation.createHTMLDocument('Title');
var p = doc.createElement('p');
p.innerHTML = 'hello world';
doc.body.appendChild(p);

document.replaceChild(
    doc.documentElement,
    document.documentElement
);

 

note: 

1. the document.implementation returns an instance object DOMImplementation.

2. DOMImplementation object has three methods, namely to create three different document types: 

// DOMImplementation.createDocument (): create an XML document. 
// DOMImplementation.createHTMLDocument (): Create an HTML document. 
// DOMImplementation.createDocumentType (): Creates a DocumentType object.

 

Guess you like

Origin www.cnblogs.com/aisowe/p/11541656.html