Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建一个javascript文档对象_Javascript_Object_Document - Fatal编程技术网

创建一个javascript文档对象

创建一个javascript文档对象,javascript,object,document,Javascript,Object,Document,有没有办法通过调用函数来创建或重新创建javascript文档对象。差不多 <script type="javascript/text"> var document = createDocument("some html"); </script> var document=createDocument(“一些html”); 我想这样做,以便我可以解决这个问题中的问题您可以尝试使用。拥有文档后,可以使用innerHTML属性为其设置HTML。如果你想把它包在一个整

有没有办法通过调用函数来创建或重新创建javascript文档对象。差不多

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

var document=createDocument(“一些html”);
我想这样做,以便我可以解决这个问题中的问题

您可以尝试使用。拥有文档后,可以使用
innerHTML
属性为其设置HTML。如果你想把它包在一个整洁的小包裹里,你可以这样做:

function createDocument(html) {
    var doc = document.implementation.createDocument ('http://www.w3.org/1999/xhtml', 'html',  null);
    doc.documentElement.innerHTML = html;
    return doc;
}
var doc = createDocument("<body><span>Hello StackOverflow.com!</span></body>");
然后你会使用这样的函数:

function createDocument(html) {
    var doc = document.implementation.createDocument ('http://www.w3.org/1999/xhtml', 'html',  null);
    doc.documentElement.innerHTML = html;
    return doc;
}
var doc = createDocument("<body><span>Hello StackOverflow.com!</span></body>");
var doc=createDocument(“Hello StackOverflow.com!”);
让我知道这是否是您想要的。

这在Firefox中有效:

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

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

如果您希望重新创建一个文档(比如在iframe中),您可以使用

document.open();
document.write('<html><head></head><body>some stuff</body></html>');
document.close();
document.open();
写一些东西;
document.close();
下面是如何使用它重新创建动态创建的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();
var-iframe=document.createElement('iframe'),
iframeDoc=(iframe.contentDocument)
? iframe.contentDocument:iframe.contentWindow.document;
document.getElementById('iframeContainer').appendChild(iframe);
iframeDoc.open();
iframeDoc.write('howdy');
iframeDoc.close();

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

document.implementation.createHTMLDocument(title);
来自版本4的Firefox也实现了此方法,而对于以前的版本,可以使用以下方法创建HTML文档:

var doc = document.implementation.createDocument('', '',
  document.implementation.createDocumentType('html', '', ''));
这应该大致相当于一个具有
(HTML5)的文档

将“createDocumentType”的空字符串替换为所需的publicId/systemId

仍然需要在生成的文档中创建/附加html、head和body元素,以使DOM正常工作。

如果createDocument(…)给出了解析错误,请修改Dan的答案,改为使用createHTMLDocument()

用作:

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

好的,但是如果
文档
未定义怎么办?有没有办法在nihilo之前创建文档?肯定有构造函数吗?@user1269964然后使用
dominimplementation
而不是
document.implementation
。编辑:对不起。不是
dom实施
,而是
(新文档())。实施
@PaulRoub此问题和相关答案日期为2011年9月,副本为2011年11月的另一个问题。副本应以出版日期为准,最早的为原件,请修复。