创建一个javascript文档对象[复制]

时间:2021-08-28 01:21:35

This question already has an answer here:

这个问题在这里已有答案:

Is there any way to create or recreate a javascript document Object by calling a function. Something like

有没有办法通过调用函数来创建或重新创建一个javascript文档对象。就像是

<script type="javascript/text">
  var document = createDocument("some html");
</script>

I want to do this so I can solve the issue in this question client side xslt with javascript in firefox

我想这样做,所以我可以在这个问题客户端xslt解决问题与Firefox中的JavaScript

5 个解决方案

#1


Webkit was the first to include/expose the following method for that task:

Webkit是第一个为该任务包含/公开以下方法的人:

document.implementation.createHTMLDocument(title);

Firefox, from version 4, also implements this method while for previous versions it is possible to create an HTML document using the following:

从版本4开始,Firefox也实现了此方法,而对于以前的版本,可以使用以下方法创建HTML文档:

var doc = document.implementation.createDocument('', '',
  document.implementation.createDocumentType('html', '', ''));

which should be roughly equivalent to a document having <!DOCTYPE html> (HTML5).

这应该大致相当于具有(HTML5)的文档。

Replace the empty strings of 'createDocumentType' with the needed publicId/systemId.

用所需的publicId / systemId替换'createDocumentType'的空字符串。

It will be still necessary to create/append html, head and body elements to the resulting document to have a working DOM.

仍然需要在生成的文档中创建/附加html,head和body元素以使其具有可用的DOM。

#2


You could try using document.implementation.createDocument. Once you have your document, you can use the innerHTML property to set HTML for it. If you want that wrapped in a neat little package you can do something like this:

您可以尝试使用document.implementation.createDocument。获得文档后,可以使用innerHTML属性为其设置HTML。如果你想用一个整齐的小包裹包裹你可以做这样的事情:

function createDocument(html) {
    var doc = document.implementation.createDocument ('http://www.w3.org/1999/xhtml', 'html',  null);
    doc.documentElement.innerHTML = html;
    return doc;
}

And then you'd use the function like this:

然后你会使用这样的函数:

var doc = createDocument("<body><span>Hello *.com!</span></body>");

Let me know if this is what you were looking for.

如果这是您正在寻找的,请告诉我。

#3


If you're looking to recreate a document (such as in an iframe) you can do so with...

如果您要重新创建文档(例如在iframe中),可以使用...

document.open();
document.write('<html><head></head><body>some stuff</body></html>');
document.close();

here is how you could use it to recreate the document of a dynamically created iframe.

以下是如何使用它来重新创建动态创建的iframe的文档。

var iframe = document.createElement('iframe'),
    iframeDoc = (iframe.contentDocument) 
              ? iframe.contentDocument : iframe.contentWindow.document;

document.getElementById('iframeContainer').appendChild(iframe);

iframeDoc.open();
iframeDoc.write('<html><head></head><body>howdy</body></html>');
iframeDoc.close();

#4


if createDocument(...) gives you parse errors, adapt Dan's answer to use createHTMLDocument() instead:

如果createDocument(...)为您提供解析错误,请调整Dan的答案以使用createHTMLDocument():

function createDocument(html, title) {
  var doc = document.implementation.createHTMLDocument(title)
  doc.documentElement.innerHTML = html
  return doc
}

use as:

var doc = createDocument('<!DOCTYPE html><html>'
    + '<head><script src="foo.js"></script></head>'
    + '<body></body></html>', 'test')
console.log(doc.getElementsByTagName('script'))

output:

[script foo.js]

#5


This works in Firefox:

这适用于Firefox:

document.implementation.createDocument(null, "rootElement", null)

Note that it gives you a XMLDocument, rather than a HTMLDocument (like document itself).

请注意,它为您提供了XMLDocument,而不是HTMLDocument(如文档本身)。

#1


Webkit was the first to include/expose the following method for that task:

Webkit是第一个为该任务包含/公开以下方法的人:

document.implementation.createHTMLDocument(title);

Firefox, from version 4, also implements this method while for previous versions it is possible to create an HTML document using the following:

从版本4开始,Firefox也实现了此方法,而对于以前的版本,可以使用以下方法创建HTML文档:

var doc = document.implementation.createDocument('', '',
  document.implementation.createDocumentType('html', '', ''));

which should be roughly equivalent to a document having <!DOCTYPE html> (HTML5).

这应该大致相当于具有(HTML5)的文档。

Replace the empty strings of 'createDocumentType' with the needed publicId/systemId.

用所需的publicId / systemId替换'createDocumentType'的空字符串。

It will be still necessary to create/append html, head and body elements to the resulting document to have a working DOM.

仍然需要在生成的文档中创建/附加html,head和body元素以使其具有可用的DOM。

#2


You could try using document.implementation.createDocument. Once you have your document, you can use the innerHTML property to set HTML for it. If you want that wrapped in a neat little package you can do something like this:

您可以尝试使用document.implementation.createDocument。获得文档后,可以使用innerHTML属性为其设置HTML。如果你想用一个整齐的小包裹包裹你可以做这样的事情:

function createDocument(html) {
    var doc = document.implementation.createDocument ('http://www.w3.org/1999/xhtml', 'html',  null);
    doc.documentElement.innerHTML = html;
    return doc;
}

And then you'd use the function like this:

然后你会使用这样的函数:

var doc = createDocument("<body><span>Hello *.com!</span></body>");

Let me know if this is what you were looking for.

如果这是您正在寻找的,请告诉我。

#3


If you're looking to recreate a document (such as in an iframe) you can do so with...

如果您要重新创建文档(例如在iframe中),可以使用...

document.open();
document.write('<html><head></head><body>some stuff</body></html>');
document.close();

here is how you could use it to recreate the document of a dynamically created iframe.

以下是如何使用它来重新创建动态创建的iframe的文档。

var iframe = document.createElement('iframe'),
    iframeDoc = (iframe.contentDocument) 
              ? iframe.contentDocument : iframe.contentWindow.document;

document.getElementById('iframeContainer').appendChild(iframe);

iframeDoc.open();
iframeDoc.write('<html><head></head><body>howdy</body></html>');
iframeDoc.close();

#4


if createDocument(...) gives you parse errors, adapt Dan's answer to use createHTMLDocument() instead:

如果createDocument(...)为您提供解析错误,请调整Dan的答案以使用createHTMLDocument():

function createDocument(html, title) {
  var doc = document.implementation.createHTMLDocument(title)
  doc.documentElement.innerHTML = html
  return doc
}

use as:

var doc = createDocument('<!DOCTYPE html><html>'
    + '<head><script src="foo.js"></script></head>'
    + '<body></body></html>', 'test')
console.log(doc.getElementsByTagName('script'))

output:

[script foo.js]

#5


This works in Firefox:

这适用于Firefox:

document.implementation.createDocument(null, "rootElement", null)

Note that it gives you a XMLDocument, rather than a HTMLDocument (like document itself).

请注意,它为您提供了XMLDocument,而不是HTMLDocument(如文档本身)。