如何使用 JavaScript 创建 Document 对象 [英] How to create Document objects with JavaScript
问题描述
基本上这是一个问题,应该如何构造一个 Document 对象来自javascript中动态的一串HTML?
Basically that's the question, how is one supposed to construct a Document object from a string of HTML dynamically in javascript?
推荐答案
规范中定义了两种方法,createDocument
来自 DOM Core Level 2 和 createHTMLDocument
来自 HTML5.前者创建一个 XML 文档(包括 XHTML),后者创建一个 HTML 文档.两者都作为函数驻留在 DOMImplementation
接口上.
There are two methods defined in specifications, createDocument
from DOM Core Level 2 and createHTMLDocument
from HTML5. The former creates an XML document (including XHTML), the latter creates a HTML document. Both reside, as functions, on the DOMImplementation
interface.
var impl = document.implementation,
xmlDoc = impl.createDocument(namespaceURI, qualifiedNameStr, documentType),
htmlDoc = impl.createHTMLDocument(title);
实际上,这些方法还很年轻,仅在最近的浏览器版本中实现.根据 http://quirksmode.org 和 MDN,以下浏览器支持createHTMLDocument
:
In reality, these methods are rather young and only implemented in recent browser releases. According to http://quirksmode.org and MDN, the following browsers support createHTMLDocument
:
- Chrome 4
- 歌剧 10
- 火狐 4
- Internet Explorer 9
- Safari 4
有趣的是,您可以(有点)在旧版本的 Internet Explorer 中使用 ActiveXObject
创建 HTML 文档:
Interestingly enough, you can (kind of) create a HTML document in older versions of Internet Explorer, using ActiveXObject
:
var htmlDoc = new ActiveXObject("htmlfile");
生成的对象将是一个新文档,它可以像任何其他文档一样进行操作.
The resulting object will be a new document, which can be manipulated just like any other document.
这篇关于如何使用 JavaScript 创建 Document 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!