跳到主要内容

Document

Document 接口表示任何在浏览器中载入的网页,并作为网页内容的入口,也就是 DOM 树。Document 根据不同的文档类型。还能使用更多 API。例如 SVG。

属性继承

EventTarget <- Node <- Document

属性

Document.activeElement 只读

返回一个目前处于聚焦状态的 Element。

Document.adoptedStyleSheets (en-US)

设置用于构造文档样式表的数组。这些样式表也可与同一文档的 shadow DOM 子树共享。

Document.body

返回当前文档的 <body><frameset> 节点。

Document.characterSet 只读

返回文档正在使用的字符集。

Document.childElementCount (en-US) 只读

返回文档正在使用的子元素的数量。

Document.children (en-US) 只读

返回当前文档的子元素。

Document.compatMode 只读

指示文档是否以怪异模式(quirks)或严格模式(strict)渲染。

Document.contentType 只读

根据当前文档的 MIME 标头,返回它的 Content-Type。

Document.currentScript 只读

返回目前正在处理且不是 JavaScript 模块的 <script> 元素。

Document.doctype 只读

返回当前文档的文档类型定义(Document Type Definition,DTD)。

Document.documentElement 只读

返回当前文档的直接子节点。对于 HTML 文档,HTMLHtmlElement 对象一般代表该文档的<html> 元素。

Document.documentURI 只读

以字符串的类型,返回当前文档的路径。

Document.embeds 只读

返回一个包含当前文档的嵌入式的元素 <embed> 的 HTMLCollection。

Document.firstElementChild (en-US) 只读

返回当前文档的第一个子元素。

Document.fonts

返回当前文档的 FontFaceSet 接口。

Document.forms 只读

返回一个包含当前文档中所有表单元素 <form> 的 HTMLCollection。

Document.fullscreenElement 只读

返回文档中正处于全屏模式的元素。

Document.head 只读

返回当前文档的 <head> 元素。

Document.hidden 只读

返回一个布尔值,表明当前页面是否隐藏。

Document.images 只读

返回当前文档中所包含的图片的 HTMLCollection。

Document.implementation 只读

返回与当前文档相关联的 DOM 实现。

Document.lastElementChild (en-US) 只读

返回当前文档的最后一个子元素。

返回一个包含文档中所有超链接的 HTMLCollection。

Document.pictureInPictureElement 只读

返回文档中正处于画中画模式的 Element。

Document.pictureInPictureEnabled 只读

若画中画特性可用,则返回 true。

Document.plugins 只读

返回一个包含可用插件的 HTMLCollection。

Document.pointerLockElement 只读

当指针被锁定时,返回鼠标事件的目标的元素集合。若锁定正处于等待状态、指针已被解锁,或是目标正处于另一个文档中,则返回 null。

Document.scripts 只读

返回包含文档中所有的 <script> 元素的 HTMLCollection。

Document.scrollingElement 只读

返回对滚动文档的 Element 的引用。

Document.styleSheets 只读

返回一个包含显式链接或嵌入到文档中的 CSSStyleSheet 对象的 StyleSheetList。

Document.timeline 只读

返回 DocumentTimeline (en-US) 的一个实例,该实例是在页面加载时自动创建的。

Document.visibilityState 只读

返回一个 string,表明当前文档的可见性。可能的取值有 visible、hidden、prerender 和 unloaded。

HTMLDocument 的扩展

HTML 文件的 ### Document 接口继承自 HTMLDocument 接口,或扩展了这些方法:

Document.cookie

返回一个使用分号分隔的 cookie 列表,或设置(写入)一个 cookie。

Document.defaultView 只读

返回一个对 window 对象的引用。

Document.designMode

获取或设置编辑整个文档的能力。

Document.dir

获取或设置文档的文字方向(rtl 或 ltr)。

Document.lastModified 只读

返回文档最后修改的时间。

Document.location 只读

返回当前文档的 URI。

Document.readyState 只读

返回当前文档的加载状态。

Document.referrer 只读

返回来源页面的 URI。

Document.title

获取或设置当前文档的标题。

Document.URL 只读

以字符串形式返回文档的地址栏链接。

方法

该接口同样继承了 Node 和 EventTarget 接口。

Document.adoptNode()

从外部文档中采用的节点。

Document.append() (en-US)

在文档的最后一个子节点后插入一个 Node 对象或字符串对象的集合。

Document.caretPositionFromPoint()

返回一个包含 DOM 节点(包含插入符号以及该符号在节点中的字符偏移量)的 CaretPosition 对象。

Document.createAttribute()

创建一个新的 Attr 对象并返回。

Document.createAttributeNS() (en-US)

在给定命名空间创建一个新的属性节点并返回。

Document.createCDATASection()

创建一个新的数据(CDATA)节点并返回。

Document.createComment()

创建一个新的注释节点并返回。

Document.createDocumentFragment()

创建一个新的文档片段。

Document.createElement()

用给定标签名创建一个新的元素。

Document.createElementNS()

用给定标签名和命名空间创建一个新的元素。

Document.createEvent()

创建一个事件对象。

Document.createNodeIterator()

创建一个 NodeIterator 对象。

Document.createProcessingInstruction()

创建一个新的 ProcessingInstruction (en-US) 对象。

Document.createRange()

创建一个 Range 对象。

Document.createTextNode()

创建一个文本节点。

Document.createTreeWalker()

创建一个 TreeWalker 对象。

Document.elementFromPoint()

返回指定坐标最顶层的元素。

Document.elementsFromPoint()

返回包含指定坐标下所有元素的数组。

Document.exitPictureInPicture()

从浮动的画中画窗口中移除视频,并返回到它的原始容器。

Document.exitPointerLock()

释放指针锁。

Document.getAnimations() (en-US)

返回包含所有目前有效的 Animation 对象(其目标元素为 document)的数组。

Document.getElementById()

返回标识元素的引用对象。

Document.getElementsByClassName()

返回具有给定类名的元素列表。

Document.getElementsByTagName()

返回具有给定标签名的元素列表('*')代表全部。

Document.getElementsByTagNameNS()

返回具有给定标签名和命名空间的元素列表。

Document.getSelection() (en-US)

返回一个 Selection 对象,表示用户选择的文本范围或是插入符号当前的位置。

Document.hasStorageAccess()

返回一个 Promise,会兑现一个布尔值,用于表示文档是否有访问第一方储存的权限。

Document.importNode()

返回外部文档的节点的拷贝。

Document.prepend() (en-US)

在文档的第一个子节点前插入一个 Node 对象或字符串对象的集合。

Document.querySelector()

返回文档中与指定的选择器匹配的第一个元素节点。

Document.querySelectorAll()

返回包含文档中与指定的选择器匹配的所有元素节点的列表。

Document.replaceChildren() (en-US)

用一哥指定的新的子节点集合替换替换文档中现有的子节点。

Document.requestStorageAccess() (en-US)

返回一个 Promise,若第一方存储权限可用,则兑现,否则拒绝。

Document 接口使用 XPathEvaluator 接口扩展:

Document.createExpression()

编译一个 XPathExpression (en-US),以用于(重复)执行。

Document.createNSResolver() (en-US)

创建一个 XPathNSResolver (en-US) 对象。

Document.evaluate()

执行一个 XPath 表达式。

HTML 文档的扩展

HTML 文档的 ### Document 接口继承自 HTMLDocument 接口,或扩展了这些方法:

Document.close()

关闭用于写入的文档流。

Document.getElementsByName()

返回一个具有给定名称的元素的列表。

Document.hasFocus()

若焦点目前位于给定的文档内,则返回 true。

Document.open()

打开用于写入的文档流。

Document.write()

向文档写入文本。

Document.writeln()

向文档写入一行文本。