为什么自定义文档对象中缺少document.write?

时间:2014-12-11 13:19:40

标签: javascript

创建自定义文档对象:

var doc = document.implementation.createDocument(
  'http://www.w3.org/1999/xhtml',
  'html',
  //Inherit current doctype
  document.doctype
);

尝试写入:

//Throws error - TypeError: doc.write is not a function
doc.write(document.documentElement.innerHTML);

同样适用于.close.open。我勒个去?这是一个错误吗?可能不是 - 所有浏览器都这样做:

Chrome (有史以来最糟糕的错误,Chrome调试必须很有趣):

Uncaught TypeError: undefined is not a function

歌剧:

TypeError: Object #<Document> has no method 'write'

火狐:

TypeError: document.implementation.createDocument(...).write is not a function

为什么自定义文档缺少方法openwriteclose

1 个答案:

答案 0 :(得分:3)

document.implementation.createDocument返回XMLDocument 但是,.write是来自documentinherits HTMLDocument函数。

基本上,XMLDocument没有这个功能 你可能正在寻找createHTMLDocument