如何使用JavaScript创建文档对象 [英] How to create Document objects with JavaScript

查看:112
本文介绍了如何使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆