Document 类型

136 阅读4分钟

Document

Document 类型是 JavaScript 中表示文档节点的类型。在浏览器中,文档对象 document 是HTMLDocument 的实例( HTMLDocument 继承 Document ),表示整个 HTML页面。 document 是 window对象的属性,因此是一个全局对象。 Document 类型的节点有以下特征:

  • nodeType 等于 9;
  • nodeName 值为 "#document" ;
  • nodeValue 值为 null ;
  • parentNode 值为 null ;
  • ownerDocument 值为 null ;
  • 子节点可以是 DocumentType (最多一个)、 Element (最多一个)、 ProcessingInstruction
  • 或 Comment 类型。

文档属性

文档属性名称说明
Document.body返回当前文档的 < body > 或 < frameset > 节点。
document.activeElement当前获得焦点的元素
Document.characterSet返回文档正在使用的字符集。
document.inputEncoding返回用于文档的编码方式(在解析时)。
Document.doctype只读,返回当前文档的文档类型定义(Document Type Definition, DTD)。
Document.documentElement只读返回当前文档的直接子节点。对于 HTML 文档,HTMLHtmlElement 对象一般代表该文档的 < html > 元素。
Document.documentURI只读以字符串的类型,返回当前文档的路径。
Document.embeds只读以列表(list)的类型,返回当前文档的嵌入式的元素 < embed > 。
Document.fonts返回当前文档的 FontFaceSet 接口。
Document.forms只读返回一个包含当前文档中所有表单元素 < form > 的列表。
Document.head只读返回当前文档的 < head > 元素。
Document.hidden只读返回一个布尔值,表明当前页面是否隐藏。
Document.images只读返回当前文档中所包含的图片的列表。
Document.implementation只读返回与当前文档相关联的 DOM 实现。
Document.lastStyleSheetSet只读返回最后启用样式表的名字。在设置selectedStyleSheetSet 前值都为 null 。
Document.links只读返回一个包含文档中所有超链接的列表。
Document.plugins只读返回一个可用插件列表。
Document.preferredStyleSheetSet只读返回 preferred style sheet set as specified by the page author.
Document.scripts只读返回文档中所有的 < script > 元素。
Document.scrollingElement只读返回对文档 Element 元素的引用。
Document.selectedStyleSheetSet返回当前正使用的样式表集。
Document.styleSheetSets只读返回文档上可用样式表的列表。
Document.timeline只读返回 DocumentTimeline(en - US) 的一个实例,该实例是在页面加载时自动创建的。
Document.visibilityState只读返回 string 表明当前文档的可见性。可能的取值有 visible, hidden, prerender, and unloaded 。
Document.cookie返回一个使用分号分隔的 cookie 列表,或设置(写入)一个 cookie。
Document.defaultView只读返回一个对(当前) window 对象的引用。
Document.designMode获取或设置(让用户)编辑整个文档的能力。
Document.dir只读获取或设置文档的文字方向(rtl 或 ltr)。
Document.domain获取或设置当前文档的域名。
Document.lastModified只读返回文档最后修改的时间。
Document.location只读返回当前文档的 URI。
Document.readyState只读返回当前文档的加载状态。
Document.referrer只读返回来源页面的 URI。
Document.title获取或设置当前文档的标题。
Document.URL只读以字符串形式返回文档的地址栏链接。

文档方法

文档方法名称说明
document.addEventListener()向文档添加句柄
document.adoptNode(node)从另外一个文档返回 adapded 节点到当前文档。
document.close()关闭用 document.open() 方法打开的输出流,并显示选定的数据。
document.createAttribute()创建一个属性节点
document.createComment()方法可创建注释节点。
document.createDocumentFragment()创建空的 DocumentFragment 对象,并返回此对象。操作dom的效率高
document.createElement()创建元素节点。
document.createTextNode()创建文本节点。
document.createCDATASection()创建一个新的数据节点(CDATA node)并返回。
document.createElementNS()用给定标签名 tagName 和命名空间创建一个新的元素。
document.createEvent()创建一个 event 对象。
document.createNodeIterator()创建一个 NodeIterator 对象。
document.createRange()创建一个 Range 对象。
document.getElementsByClassName()返回文档中所有指定类名的元素集合,作为 NodeList 对象。
document.getElementById()返回对拥有指定 id 的第一个对象的引用。
document.getElementsByName()返回带有指定名称的对象集合。
document.getElementsByTagName()返回带有指定标签名的对象集合。
document.importNode()把一个节点从另一个文档复制到该文档以便应用。
document.normalize()删除空文本节点,并连接相邻节点
document.normalizeDocument()删除空文本节点,并连接相邻节点的
document.open()打开一个流,以收集来自任何 document.write() 或 document.writeln() 方法的输出。
document.querySelector()返回文档中匹配指定的CSS选择器的第一元素
document.querySelectorAll()是 HTML5中引入的新方法,返回文档中匹配的CSS选择器的所有元素节点列表
document.removeEventListener()移除文档中的事件句柄(由 addEventListener() 方法添加)
document.renameNode()重命名元素或者属性节点。
document.write()向文档写 HTML 表达式 或 JavaScript 代码。
document.writeln()等同于 write() 方法,不同的是在每个表达式之后写一个换行符。