如何使用JavaScript创建文档对象 [英] How to create Document objects with JavaScript
问题描述
在规范中定义了两种方法, createDocument
来自DOM Core Level 2和 createHTMLDocument
来自HTML5。前者创建一个XML文档(包括XHTML),后者创建一个HTML文档。两个作为函数驻留在 DOMImplementation
界面上。
var impl = document.implementation,
xmlDoc = impl.createDocument(namespaceURI,qualifiedNameStr,documentType),
htmlDoc = impl.createHTMLDocument(title);
实际上,这些方法相当年轻,只在最近的浏览器版本中实现。根据 http://quirksmode.org 和 MDN ,以下浏览器支持 createHTMLDocument
:
- Chrome 4
- Opera 10
- Firefox 4
- Internet Explorer 9
<有趣的是,您可以(以某种方式)在旧版本的Internet Explorer中创建一个HTML文档,使用<$($)
code> ActiveXObject :
var htmlDoc = new ActiveXObject(htmlfile);
生成的对象将是一个新的文档,可以像任何其他文档一样被操纵。 p>
Basically that's the question, how is one supposed to construct a Document object from a string of HTML dynamically in javascript?
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);
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
- Opera 10
- Firefox 4
- Internet Explorer 9
- Safari 4
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创建文档对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!