DOM
Document 接口表示任何在浏览器中载入的 Web 页面,并作为 Web 页面内容的入口,也就是 DOM 树。
DOM 树包含了像 body、table 这样的元素,以及大量其他元素。它向文档本身提供了全局操作功能,能解决如何获取页面的 URL,如何在文档中创建一个新的元素这样的问题。
Document 接口描述了任何类型的文档的通用属性与方法。根据不同的文档类型(例如 HTML、XML、SVG……),还有更多可用的 API:使用 "text/html" 作为内容类型的 HTML 文档,还实现了 HTMLDocument 接口,而 XML 和 SVG 文档则实现了 XMLDocument 接口。
构造函数
- Document.Document
- : 创建一个新的
Document对象。
- : 创建一个新的
实例属性
此接口还继承了 Node 和 EventTarget 接口。
- Document.activeElement
- : 返回目前处于聚焦状态的 Element。
- Document.activeViewTransition
- : 返回一个表示当前在文档上活动的视图转换的 ViewTransition 实例,如果没有活动的视图转换则返回
null。
- : 返回一个表示当前在文档上活动的视图转换的 ViewTransition 实例,如果没有活动的视图转换则返回
- Document.adoptedStyleSheets
- : 添加用于构造文档样式表的数组。这些样式表也可与同一文档的影子 DOM 子树共享。
- Document.body
- : 返回当前文档的 body 或 frameset 节点。
- Document.characterSet
- : 返回文档正在使用的字符集。
- Document.childElementCount
- : 返回当前文档的子元素数量。
- Document.children
- : 返回当前文档的子元素。
- Document.compatMode
- : 指示文档是否以怪异模式(quirks)或严格模式(strict)渲染。
- Document.contentType
- : 根据当前文档的 MIME 标头,返回其内容类型(Content-Type)。
- Document.currentScript
- : 返回目前正在处理且不是 JavaScript 模块的 script 元素。
- Document.doctype
- : 返回当前文档的文档类型定义(Document Type Definition,DTD)。
- Document.documentElement
- : 返回当前文档的一个直接子节点Element。对于 HTML 文档,一般为代表该文档的 html 元素的 HTMLHtmlElement 对象。
- Document.documentURI
- : 返回文档的位置(字符串形式)。
- Document.embeds
- : 返回包含文档中所有嵌入式元素 embed 的 HTMLCollection。
- Document.featurePolicy
- : 返回表示应用于文档的特性策略的 FeaturePolicy 接口。
- Document.firstElementChild
- : 返回当前文档的第一个子元素。
- Document.fonts
- : 返回当前文档的 FontFaceSet 接口。
- Document.forms
- : 返回包含文档中所有表单元素 form 的 HTMLCollection。
- Document.fragmentDirective
- : 返回当前文档的 FragmentDirective。
- Document.fullscreenElement
- : 返回文档中正处于全屏模式的元素。
- Document.head
- : 返回当前文档的 head 元素。
- Document.hidden
- : 返回一个表明当前页面是否隐藏的布尔值。
- Document.images
- : 返回包含文档中所有图片的 HTMLCollection。
- Document.implementation
- : 返回与当前文档相关联的 DOM 实现。
- Document.lastElementChild
- : 返回当前文档的最后一个子元素。
- Document.links
- : 返回包含文档中所有超链接的 HTMLCollection。
- Document.pictureInPictureElement
- : 返回文档中正处于画中画模式的Element。
- Document.pictureInPictureEnabled
- : 若画中画特性可用,则返回
true。
- : 若画中画特性可用,则返回
- Document.plugins
- : 返回一个包含可用插件的 HTMLCollection。
- Document.pointerLockElement
- : 当指针被锁定时,返回鼠标事件的目标的元素集合。若锁定正处于等待状态、指针已被解锁,或是目标正处于另一个文档中,则返回
null。
- : 当指针被锁定时,返回鼠标事件的目标的元素集合。若锁定正处于等待状态、指针已被解锁,或是目标正处于另一个文档中,则返回
- Document.prerendering
- : 返回一个表示文档当前是否处于预渲染过程(通过推测规则 API 启动)中的布尔值。
- Document.scripts
- : 返回包含文档中所有的 script 元素的 HTMLCollection。
- Document.scrollingElement
- : 返回对用于滚动文档的Element的引用。
- Document.styleSheets
- : 返回一个包含显式链接或嵌入到文档中的 CSSStyleSheet 对象的 StyleSheetList。
- Document.timeline
- : 返回一个在页面加载时自动创建的特殊 DocumentTimeline 实例。
- 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.fullscreenEnabled
- : 表示全屏模式是否可用。
- Document.lastModified
- : 返回文档最后修改的时间。
- Document.location
- : 返回当前文档的 URI。
- Document.readyState
- : 返回当前文档的加载状态。
- Document.referrer
- : 返回链接到此页的页面的 URI。
- Document.title
- : 获取或设置当前文档的标题。
- Document.URL
- : 以字符串形式返回文档的位置。
- 具名属性
-
: 文档中的某些元素也以属性的形式暴露:
- 对于每个 embed、form、iframe、img 和 object 元素,其
name(如果非空)都会被暴露出来。例如,如果文档包含<iframe name="my_form">,那么document["my_form"](及其等价的document.my_form)会返回对该元素的引用。 - 对于每个 form 元素,其
id(如果非空)也会被暴露出来。 - 对于每个具有非空
name的 img 元素,其id(如果非空)也会被暴露出来。
如果某个属性对应单个元素,则直接返回该元素。如果该单个元素是一个 iframe,则返回其 HTMLIFrameElement/contentWindow 代替。如果该属性对应多个元素,则返回一个包含所有这些元素的 HTMLCollection。
- 对于每个 embed、form、iframe、img 和 object 元素,其
-
已弃用的属性
- Document.alinkColor
- : 返回或设置文档正文部分激活的链接的颜色。
- Document.all
- : 返回一个以文档节点为根节点的 HTMLAllCollection 集合,以访问文档中所有的元素。这是遗留的非标准属性,不应使用。
- Document.anchors
- : 返回文档中所有锚点元素的列表。
- Document.applets
- : 返回一个空的 HTMLCollection。过去用于返回文档中的小应用程序(applet)列表的遗留属性。
- Document.bgColor
- : 获取或设置当前文档的背景颜色。
- Document.characterSet
- : Document.characterSet 的别名,请使用原始属性替换。
- Document.domain
- : 获取或设置当前文档的域。
- Document.fgColor
- : 获取或设置当前文档的前景色或文本颜色。
- Document.fullscreen
- : 若文档处于全屏模式,则返回
true。
- : 若文档处于全屏模式,则返回
- Document.inputEncoding
- : Document.characterSet 的别名,请使用原始属性替换。
- Document.lastStyleSheetSet
- : 返回最后启用的样式表的名字。在设置 document.selectedStyleSheetSet 前,其值都为
null。
- : 返回最后启用的样式表的名字。在设置 document.selectedStyleSheetSet 前,其值都为
- Document.linkColor
- : 获取或设置文档中超链接的颜色。
- Document.preferredStyleSheetSet
- : 返回文档作者首选的样式表。
- Document.rootElement
- : 类似于 Document.documentElement,但其仅用于 svg 根元素。请使用后者代替。
- Document.selectedStyleSheetSet
- : 返回当前正使用的样式表的集合。
- Document.styleSheetSets
- : 返回文档上可用样式表的集合。
- Document.vlinkColor
- : 获取或设置被访问的超链接的颜色。
- Document.xmlEncoding
- : 返回由 XML 声明的编码类型。
Document.xmlStandalone- : 若 XML 声明指定的文档是独立的(例如:文档类型定义的外部内容会影响文档的内容),则返回
true,否则返回false。
- : 若 XML 声明指定的文档是独立的(例如:文档类型定义的外部内容会影响文档的内容),则返回
- Document.xmlVersion
- : 返回 XML 声明中指定的版本号,若声明不存在则为
"1.0"。
- : 返回 XML 声明中指定的版本号,若声明不存在则为
实例方法
该接口还继承了 Node 和 EventTarget 接口。
- Document.adoptNode()
- : 从外部文档中转移节点。
- Document.append()
- : 在文档的最后一个子节点后插入一个 Node 对象或字符串的集合。
- Document.ariaNotify()
- : 指定屏幕阅读器应朗读给定的文本字符串。
- Document.browsingTopics()
- : 返回一个会兑现为对象数组(表示用户最近三个周期中的热门主题)的 promise。默认情况下,该方法还会使浏览器记录调用者观察到的当前页面访问信息,以便稍后在主题计算中使用页面的主机名。有关更多详细信息,请参阅主题 API。
Document.captureEvents()- : 参见 Window.captureEvents。
- Document.caretPositionFromPoint()
- : 返回一个包含 DOM 节点(包含插入符号以及该符号在节点中的字符偏移量)的 CaretPosition 对象。
- Document.caretRangeFromPoint()
- : 获取一个 Range 对象,其为指定坐标下的文档片段。
- Document.createAttribute()
- : 创建一个新的 Attr 对象并返回。
- Document.createAttributeNS()
- : 在给定命名空间创建一个新的属性节点并返回。
- Document.createCDATASection()
- : 创建一个新的数据(CDATA)节点并返回。
- Document.createComment()
- : 创建一个新的注释节点并返回。
- Document.createDocumentFragment()
- : 创建一个新的文档片段。
- Document.createElement()
- : 用给定标签名创建一个新的元素。
- Document.createElementNS()
- : 用给定标签名和命名空间创建一个新的元素。
- Document.createEvent()
- : 创建一个事件对象。
- Document.createNodeIterator()
- : 创建一个 NodeIterator 对象。
- Document.createProcessingInstruction()
- : 创建一个新的 ProcessingInstruction 对象。
- Document.createRange()
- : 创建一个 Range 对象。
- Document.createTextNode()
- : 创建一个文本节点。
- Document.createTouch()
- : 创建一个 Touch 对象。
- Document.createTouchList()
- : 创建一个 TouchList 对象。
- Document.createTreeWalker()
- : 创建一个 TreeWalker 对象。
- Document.elementFromPoint()
- : 返回指定坐标最顶层的元素。
- Document.elementsFromPoint()
- : 返回包含指定坐标下所有元素的数组。
- Document.enableStyleSheetsForSet()
- : 启用指定的样式表集合。
- Document.exitFullscreen()
- : 阻止文档的全屏元素以全屏模式显示。
- Document.exitPictureInPicture()
- : 从浮动的画中画窗口中移除视频,并返回到它的原始容器。
- Document.exitPointerLock()
- : 释放指针锁。
- Document.getAnimations()
- : 返回包含所有目前有效的 Animation 对象(其目标元素为
document的后代)的数组。
- : 返回包含所有目前有效的 Animation 对象(其目标元素为
Document.getBoxQuads()- : 返回一个 DOMQuad 对象(表示节点中的 CSS 片段)的列表。
- Document.getElementById
- : 返回标识元素的引用对象。
- Document.getElementsByClassName()
- : 返回具有给定类名的元素列表。
- Document.getElementsByTagName()
- : 返回具有给定标签名的元素列表。
- Document.getElementsByTagNameNS()
- : 返回具有给定标签名和命名空间的元素列表。
- Document.getSelection()
- : 返回一个 Selection 对象,表示用户选择的文本范围或是插入符号当前的位置。
- Document.hasPrivateToken()
- : 返回一个会兑现为用于表示浏览器是否有来自特定发行者的私有状态令牌的布尔值的 Promise。
- Document.hasRedemptionRecord()
- : 返回一个会兑现为用于表示浏览器是否有来自特定发行者的兑换记录的布尔值的 promise。
- Document.hasStorageAccess()
- : 返回一个 Promise,其会兑现一个用于表示文档是否有未分区 cookie 访问权限的布尔值。
- Document.hasUnpartitionedCookieAccess()
- : Document.hasStorageAccess() 的新名称。
- Document.importNode()
- : 返回外部文档的节点的拷贝。
- Document.moveBefore()
- : 将给定 Node 移动到
DocumentDOM 节点内(作为直接子节点)的给定参考节点之前,而不通过先移除然后再插入的方式。
- : 将给定 Node 移动到
- Document.mozSetImageElement()
- : 允许你更改指定元素 ID 的背景图像所用的元素。
- Document.prepend()
- : 在文档的第一个子节点前插入一个 Node 对象或字符串的集合。
- Document.querySelector()
- : 返回文档中与指定的选择器匹配的第一个元素节点。
- Document.querySelectorAll()
- : 返回包含文档中与指定的选择器匹配的所有元素节点的列表。
- Document.releaseCapture()
- : 若鼠标在当前文档的某一个元素之上,则释放当前的鼠标捕获。
Document.releaseEvents()- : 参见 Window.releaseEvents()。
- Document.replaceChildren()
- : 用一个指定的新的子节点集合替换替换文档中现有的子节点。
- Document.requestStorageAccess()
- : 允许在第三方上下文中加载的文档(即嵌入在 iframe 的)请求访问未分区 cookie,以便在默认情况下,用户代理可以通过阻止在第三方上下文中加载的网站访问未分区 cookie 来提高隐私性。
- Document.requestStorageAccessFor()
- : 允许顶级站点代表来自同一关联网站集合中的其他站点的嵌入内容请求第三方 cookie 访问。
- Document.startViewTransition()
- : 启动新的View Transition API并返回一个表示它的 ViewTransition 对象。
Document 接口使用 XPathEvaluator 接口扩展:
- Document.createExpression()
- : 编译一个用于(重复)执行的
XPathExpression。
- : 编译一个用于(重复)执行的
- Document.createNSResolver()
- : 原样返回输入的节点。
- Document.evaluate()
- : 执行一个 XPath 表达式。
HTML 文档的扩展
HTML 文档的 Document 接口继承了 HTMLDocument 接口,或为文档进行了扩展:
- Document.clear()
- : 这个方法没有任何作用。
- Document.close()
- : 关闭用于写入的文档流。
- Document.execCommand()
- : 在可编辑文档中执行格式化命令。
- Document.getElementsByName()
- : 返回一个具有给定名称的元素的列表。
- Document.hasFocus()
- : 若焦点目前位于给定的文档内,则返回
true。
- : 若焦点目前位于给定的文档内,则返回
- Document.open()
- : 打开用于写入的文档流。
- Document.queryCommandEnabled()
- : 若可在当前范围内执行格式化命令,则返回
true。
- : 若可在当前范围内执行格式化命令,则返回
Document.queryCommandIndeterm()- : 若格式化命令在当前范围内处于不确定的状态,则返回
true。
- : 若格式化命令在当前范围内处于不确定的状态,则返回
- Document.queryCommandState()
- : 若格式化命令已在当前范围内执行,则返回
true。
- : 若格式化命令已在当前范围内执行,则返回
- Document.queryCommandSupported()
- : 若当前范围支持格式化命令,则返回
true。
- : 若当前范围支持格式化命令,则返回
Document.queryCommandValue()- : 返回格式化命令当前的范围值。
- Document.write()
- : 向文档写入文本。
- Document.writeln()
- : 向文档写入一行文本。
静态方法
此接口还继承了 Node 和 EventTarget 接口。
- Document/parseHTML_static
- : 以 XSS 安全的方式,通过对 HTML 字符串进行净化处理,创建一个新的
Document对象。
- : 以 XSS 安全的方式,通过对 HTML 字符串进行净化处理,创建一个新的
- Document/parseHTMLUnsafe_static
- : 通过 HTML 字符串创建一个新的
Document对象,而不执行任何清理操作。该字符串可能包含声明式影子根。
- : 通过 HTML 字符串创建一个新的
事件
可以使用 addEventListener() 或为该接口的事件处理器属性 oneventname 赋值的方式来监听这些事件。除了下面列出的事件之外,许多事件还可以在文档树中包含的Node冒泡。
- Document.afterscriptexecute_event
- : 在静态的 script 元素执行完其中的脚本时触发。
- Document.beforescriptexecute_event
- : 在静态的 script 开始执行脚本时触发。
- Document.prerenderingchange_event
- : 在预渲染文档被激活(即用户查看该页面)时触发。
- Document.securitypolicyviolation_event
- : 在违反内容安全策略时触发。
- Document/visibilitychange_event
- : 在标签页内容变为可见或隐藏时触发。
全屏事件
- Document/fullscreenchange_event
- : 在
Document进入或退出全屏模式时触发。
- : 在
- Document/fullscreenerror_event
- : 在尝试进入或退出全屏模式而发生错误时触发。
加载和卸载事件
- Document/DOMContentLoaded_event
- : 在文档完全加载并解析后触发,无需等待样式表、图像和子框架完成加载。
- Document/readystatechange_event
- : 在文档的 Document/readyState 属性发生变化时触发。
指针锁定事件
- Document/pointerlockchange_event
- : 在指针被锁定或解锁时触发。
- Document/pointerlockerror_event
- : 在尝试锁定指针失败时触发。
滚动事件
- Document/scroll_event
- : 在文档视图或元素被滚动时触发。
- Document/scrollend_event
- : 在文档视图或元素完成滚动时触发。
- Document/scrollsnapchange_event
- : 在滚动操作结束而选择了新的滚动捕捉目标时,在滚动容器上触发。
- Document/scrollsnapchanging_event
- : 在浏览器确定有新的滚动捕捉目标待定(即若当前滚动手势结束,则会被选中)时,在滚动容器上触发。
选择事件
- Document/selectionchange_event
- : 在文档中的选中的文本发生改变时触发。
冒泡事件
并非所有冒泡事件都能到达 Document 对象。只有以下事件可以,并且可以在 Document 对象上监听:
abort- Element/auxclick_event
- Element/beforeinput_event
- Element/beforematch_event
- HTMLElement/beforetoggle_event
- Element/blur_event
cancel- HTMLMediaElement/canplay_event
- HTMLMediaElement/canplaythrough_event
- HTMLElement/change_event
- Element/click_event
- HTMLDialogElement/close_event
- HTMLCanvasElement/contextlost_event
- Element/contextmenu_event
- HTMLCanvasElement/contextrestored_event
- Element/copy_event
- HTMLTrackElement/cuechange_event
- Element/cut_event
- Element/dblclick_event
- HTMLElement/drag_event
- HTMLElement/dragend_event
- HTMLElement/dragenter_event
- HTMLElement/dragleave_event
- HTMLElement/dragover_event
- HTMLElement/dragstart_event
- HTMLElement/drop_event
- HTMLMediaElement/durationchange_event
- HTMLMediaElement/emptied_event
- HTMLMediaElement/ended_event
- HTMLElement/error_event
- Element/focus_event
- HTMLFormElement/formdata_event
- Element/input_event
- HTMLInputElement/invalid_event
- Element/keydown_event
- Element/keypress_event
- Element/keyup_event
- HTMLElement/load_event
- HTMLMediaElement/loadeddata_event
- HTMLMediaElement/loadedmetadata_event
- HTMLMediaElement/loadstart_event
- Element/mousedown_event
- Element/mouseenter_event
- Element/mouseleave_event
- Element/mousemove_event
- Element/mouseout_event
- Element/mouseover_event
- Element/mouseup_event
- Element/paste_event
- HTMLMediaElement/pause_event
- HTMLMediaElement/play_event
- HTMLMediaElement/playing_event
- HTMLMediaElement/progress_event
- HTMLMediaElement/ratechange_event
- HTMLFormElement/reset_event
- HTMLVideoElement/resize_event
- Element/scroll_event
- Element/scrollend_event
- Element/securitypolicyviolation_event
- HTMLMediaElement/seeked_event
- HTMLMediaElement/seeking_event
- HTMLInputElement/select_event
- HTMLSlotElement/slotchange_event
- HTMLMediaElement/stalled_event
- HTMLFormElement/submit_event
- HTMLMediaElement/suspend_event
- HTMLMediaElement/timeupdate_event
- HTMLElement/toggle_event
- HTMLMediaElement/volumechange_event
- HTMLMediaElement/waiting_event
- Element/wheel_event
规范
浏览器兼容性
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
