--- title: Document slug: Web/API/Document tags: - API - DOM - Document - 参考 - 接口 translation_of: Web/API/Document --- <div>{{APIRef("DOM")}}</div> <p><strong><code>Document</code></strong> 接口表示任何在浏览器中载入的网页,并作为网页内容的入口,也就是<a href="https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core">DOM 树</a>。DOM 树包含了像 {{HTMLElement("body")}} 、{{HTMLElement("table")}} 这样的元素,以及<a href="/zh-CN/docs/Web/HTML/Element">大量其他元素</a>。它向网页文档本身提供了全局操作功能,能解决如何获取页面的 URL ,如何在文档中创建一个新的元素这样的问题。</p> <p>{{inheritanceDiagram}}</p> <p><code>Document</code> 接口描述了任何类型的文档的通用属性与方法。根据不同的文档类型(例如<a href="/en-US/docs/HTML" title="HTML">HTML</a>、<a href="/en-US/docs/XML" title="XML">XML</a>、<a href="/zh-CN/docs/Web/SVG">SVG</a>,...),还能使用更多 API:使用 <code>"text/html"</code> 作为内容类型(content type)的 HTML 文档,还实现了 {{DOMxRef("HTMLDocument")}} 接口,而 XML 和 SVG 文档则(额外)实现了 {{DOMxRef("XMLDocument")}} 接口。</p> <h2 id="构造器">构造器</h2> <dl> <dt>{{DOMxRef("Document.Document","Document()")}}</dt> <dd>创建一个新的 <code>Document</code> 对象。</dd> </dl> <h2 id="属性">属性</h2> <p><em>此接口也继承自 {{DOMxRef("Node")}} 和 {{DOMxRef("EventTarget")}} 接口。</em></p> <dl> <dt>{{DOMxRef("Document.all")}} {{ReadOnlyInline}} {{Experimental_Inline}}</dt> <dd>返回一个以文档节点为根节点的 {{DOMxRef('HTMLAllCollection')}} 集合。换句话说,它能返回页面的完整内容。</dd> <dt>{{DOMxRef("Document.anchors")}} {{ReadOnlyInline}}</dt> <dd>返回文档中所有锚点元素的列表。</dd> <dt>{{DOMxRef("Document.body")}}</dt> <dd>返回当前文档的 {{HTMLElement("body")}} 或 {{htmlelement("frameset")}} 节点。</dd> <dt>{{DOMxRef("Document.characterSet")}} {{ReadOnlyInline}}</dt> <dd>返回文档正在使用的字符集。</dd> <dt>{{DOMxRef("Document.compatMode")}} {{ReadOnlyInline}} {{Experimental_Inline}}</dt> <dd>指示文档是否以 <em>quirks</em> 怪异模式或 <em>strict</em> 严格模式呈现。</dd> <dt>{{DOMxRef("Document.contentType")}} {{ReadOnlyInline}} {{Experimental_Inline}}</dt> <dd>根据当前文档的 MIME Header,返回它的 Content-Type。</dd> <dt>{{DOMxRef("Document.doctype")}} {{ReadOnlyInline}}</dt> <dd>返回当前文档的文档类型定义(Document Type Definition, DTD)。</dd> <dt>{{DOMxRef("Document.documentElement")}} {{ReadOnlyInline}}</dt> <dd>返回当前文档的直接子节点。对于 HTML 文档,{{DOMxRef("HTMLHtmlElement")}} 对象一般代表该文档的{{HTMLElement("html")}} 元素。</dd> <dt>{{DOMxRef("Document.documentURI")}} {{ReadOnlyInline}}</dt> <dd>以字符串的类型,返回当前文档的路径。</dd> <dt>{{DOMxRef("Document.embeds")}} {{ReadOnlyInline}}</dt> <dd>以列表(list)的类型,返回当前文档的嵌入式的元素 {{HTMLElement('embed')}} 。</dd> <dt>{{DOMxRef("Document.fonts")}}</dt> <dd>返回当前文档的 {{DOMxRef("FontFaceSet")}} 接口。</dd> <dt>{{DOMxRef("Document.forms")}} {{ReadOnlyInline}}</dt> <dd>返回一个包含当前文档中所有表单元素 {{HTMLElement("form")}} 的列表。</dd> <dt>{{DOMxRef("Document.head")}} {{ReadOnlyInline}}</dt> <dd>返回当前文档的 {{HTMLElement("head")}} 元素。</dd> <dt>{{DOMxRef("Document.hidden")}} {{ReadOnlyInline}}</dt> <dd>返回一个布尔值,表明当前页面是否隐藏。</dd> <dt>{{DOMxRef("Document.images")}} {{ReadOnlyInline}}</dt> <dd>返回当前文档中所包含的图片的列表。</dd> <dt>{{DOMxRef("Document.implementation")}} {{ReadOnlyInline}}</dt> <dd>返回与当前文档相关联的 DOM 实现。</dd> <dt>{{DOMxRef("Document.lastStyleSheetSet")}} {{ReadOnlyInline}}</dt> <dd>返回最后启用样式表的名字。在设置{{DOMxRef("document.selectedStyleSheetSet","selectedStyleSheetSet")}} 前值都为 <code>null</code> 。 </dd> <dt>{{DOMxRef("Document.links")}} {{ReadOnlyInline}}</dt> <dd>返回一个包含文档中所有超链接的列表。</dd> <dt>{{DOMxRef("Document.mozSyntheticDocument")}} {{Non-standard_Inline}}</dt> <dd>返回 {{JSxRef("Boolean")}} ,仅当此文件是合成的(例如独立图像,视频,音频文件等)时才为 <code>true</code> 。</dd> <dt>{{DOMxRef("Document.plugins")}} {{ReadOnlyInline}}</dt> <dd>返回一个可用插件列表。</dd> <dt>{{DOMxRef("Document.featurePolicy")}} {{ReadOnlyInline}} {{Experimental_Inline}}</dt> <dd>返回 {{DOMxRef("FeaturePolicy")}} interface which provides a simple API for introspecting the feature policies applied to a specific document.</dd> <dt>{{DOMxRef("Document.preferredStyleSheetSet")}} {{ReadOnlyInline}}</dt> <dd>返回 preferred style sheet set as specified by the page author.</dd> <dt>{{DOMxRef("Document.scripts")}} {{ReadOnlyInline}}</dt> <dd>返回文档中所有的 {{HTMLElement("script")}} 元素。</dd> <dt>{{DOMxRef("Document.scrollingElement")}} {{ReadOnlyInline}}</dt> <dd>返回对文档 {{DOMxRef("Element")}} 元素的引用。</dd> <dt>{{DOMxRef("Document.selectedStyleSheetSet")}}</dt> <dd>返回当前正使用的样式表集。</dd> <dt>{{DOMxRef("Document.styleSheetSets")}} {{ReadOnlyInline}}</dt> <dd>返回文档上可用样式表的列表。</dd> <dt>{{DOMxRef("Document.timeline")}} {{ReadOnlyInline}}</dt> <dd>返回 {{domxref("DocumentTimeline")}} 的一个实例,该实例是在页面加载时自动创建的。</dd> <dt>{{DOMxRef("Document.undoManager")}} {{ReadOnlyInline}} {{Experimental_Inline}}</dt> <dd>…</dd> <dt>{{DOMxRef("Document.visibilityState")}} {{ReadOnlyInline}}</dt> <dd>返回 <code>string</code> 表明当前文档的可见性。可能的取值有 <code>visible</code>, <code>hidden</code>, <code>prerender</code>, and <code>unloaded 。</code></dd> </dl> <p><code>Document</code> 接口继承自 {{DOMxRef("ParentNode")}} 的接口:</p> <p>{{page("/en-US/docs/Web/API/ParentNode","Properties")}}</p> <h3 id="HTMLDocument_的扩展">HTMLDocument 的扩展</h3> <p><em>HTML 文件的 <code>Document</code> 接口继承自</em><em> {{DOMxRef("HTMLDocument")}} 接口(从 HTML5 扩展):</em></p> <dl> <dt>{{DOMxRef("Document.cookie")}}</dt> <dd>返回一个使用分号分隔的 cookie 列表,或设置(写入)一个 cookie。</dd> <dt>{{DOMxRef("Document.defaultView")}} {{ReadOnlyInline}}</dt> <dd>返回一个对(当前) <code>window</code> 对象的引用。</dd> <dt>{{DOMxRef("Document.designMode")}}</dt> <dd>获取或设置(让用户)编辑整个文档的能力。</dd> <dt>{{DOMxRef("Document.dir")}} {{ReadOnlyInline}}</dt> <dd>获取或设置文档的文字方向(rtl 或 ltr)。</dd> <dt>{{DOMxRef("Document.domain")}}</dt> <dd>获取或设置当前文档的域名。</dd> <dt>{{DOMxRef("Document.lastModified")}} {{ReadOnlyInline}}</dt> <dd>返回文档最后修改的时间。</dd> <dt>{{DOMxRef("Document.location")}} {{ReadOnlyInline}}</dt> <dd>返回当前文档的 URI。</dd> <dt>{{DOMxRef("Document.readyState")}} {{ReadOnlyInline}}</dt> <dd>返回当前文档的加载状态。</dd> <dt>{{DOMxRef("Document.referrer")}} {{ReadOnlyInline}}</dt> <dd>返回来源页面的 URI。</dd> <dt>{{DOMxRef("Document.title")}}</dt> <dd>获取或设置当前文档的标题。</dd> <dt>{{DOMxRef("Document.URL")}} {{ReadOnlyInline}}</dt> <dd>以字符串形式返回文档的地址栏链接。</dd> </dl> <h3 id="DocumentOrShadowRoot_包含的属性">DocumentOrShadowRoot 包含的属性</h3> <p><em><code>Document</code> 接口混入(mixin){{DOMxRef("DocumentOrShadowRoot")}} 包含的属性。请注意,这些属性目前仅有 Chrome 实现;其他浏览器仍在 {{DOMxRef("Document")}} 接口上直接实现它们。.</em></p> <dl> <dt>{{DOMxRef("DocumentOrShadowRoot.activeElement")}} {{ReadOnlyInline}}</dt> <dd>返回阴影树内聚焦的 {{DOMxRef('Element')}} 。</dd> <dt>{{DOMxRef("Document.fullscreenElement")}} {{ReadOnlyInline}}</dt> <dd>当前文档处于全屏模式下的元素。</dd> <dt>{{DOMxRef("DocumentOrShadowRoot.pointerLockElement")}} {{ReadOnlyInline}} {{Experimental_Inline}}</dt> <dd>返回 element set as the target for mouse events while the pointer is locked. <code>null</code> if lock is pending, pointer is unlocked, or if the target is in another document.</dd> <dt>{{DOMxRef("DocumentOrShadowRoot.styleSheets")}} {{ReadOnlyInline}}</dt> <dd>Returns a {{DOMxRef('StyleSheetList')}} of {{DOMxRef('CSSStyleSheet')}} objects for stylesheets explicitly linked into, or embedded in a document.</dd> </dl> <h3 id="Event_handlers">Event handlers</h3> <dl> <dt>{{DOMxRef("Document.onafterscriptexecute")}} {{Non-standard_Inline}}</dt> <dd>Represents the event handling code for the {{event("afterscriptexecute")}} event.</dd> <dt>{{DOMxRef("Document.onbeforescriptexecute")}} {{Non-standard_Inline}}</dt> <dd>Represents the event handling code for the {{event("beforescriptexecute")}} event.</dd> <dt>{{DOMxRef("Document.oncopy")}} {{Non-standard_Inline}}</dt> <dd>Represents the event handling code for the {{event("copy")}} event.</dd> <dt>{{DOMxRef("Document.oncut")}} {{Non-standard_Inline}}</dt> <dd>Represents the event handling code for the {{event("cut")}} event.</dd> <dt>{{DOMxRef("Document.onfullscreenchange")}}</dt> <dd>Is an {{event("Event_handlers", "event handler")}} representing the code to be called when the {{event("fullscreenchange")}} event is raised.</dd> <dt>{{DOMxRef("Document.onfullscreenerror")}}</dt> <dd>Is an {{event("Event_handlers", "event handler")}} representing the code to be called when the {{event("fullscreenerror")}} event is raised.</dd> <dt>{{DOMxRef("Document.onpaste")}} {{Non-standard_Inline}}</dt> <dd>Represents the event handling code for the {{event("paste")}} event.</dd> <dt>{{DOMxRef("Document.onreadystatechange")}}</dt> <dd>Represents the event handling code for the {{event("readystatechange")}} event.</dd> <dt>{{DOMxRef("Document.onselectionchange")}} {{Experimental_Inline}}</dt> <dd>Is an {{event("Event_handlers", "event handler")}} representing the code to be called when the {{event("selectionchange")}} event is raised.</dd> <dt>{{DOMxRef("Document.onvisibilitychange")}}</dt> <dd>Is an {{event("Event_handlers", "event handler")}} representing the code to be called when the {{event("visibilitychange")}} event is raised.</dd> </dl> <p>The <code>Document</code> interface is extended with the {{DOMxRef("GlobalEventHandlers")}} interface:</p> <p>{{Page("/en-US/docs/Web/API/GlobalEventHandlers", "Properties")}}</p> <h3 id="Deprecated_properties">Deprecated properties</h3> <dl> <dt>{{DOMxRef("Document.alinkColor")}} {{Deprecated_Inline}}</dt> <dd>Returns or sets the color of active links in the document body.</dd> <dt>{{DOMxRef("Document.all")}} {{Deprecated_Inline}} {{Non-standard_Inline}}</dt> <dd>Provides access to all elements in the document. This is a legacy, non-standard property and should not be used.</dd> <dt>{{DOMxRef("Document.applets")}} {{Deprecated_Inline}} {{ReadOnlyInline}}</dt> <dd>Returns an ordered list of the applets within a document.</dd> <dt>{{DOMxRef("Document.bgColor")}} {{Deprecated_Inline}}</dt> <dd>获取或设置 the background color of 当前文档。</dd> <dt>{{DOMxRef("Document.charset")}} {{ReadOnlyInline}} {{Deprecated_Inline}}</dt> <dd>Alias of {{DOMxRef("Document.characterSet")}}. Use this property instead.</dd> <dt>{{DOMxRef("Document.domConfig")}} {{Deprecated_Inline}}</dt> <dd>Should return a {{DOMxRef("DOMConfiguration")}} 对象。</dd> <dt>{{DOMxRef("document.fgColor")}} {{Deprecated_Inline}}</dt> <dd>获取或设置 the foreground color, or text color, of 当前文档。</dd> <dt>{{DOMxRef("Document.fullscreen")}} {{Obsolete_Inline}}</dt> <dd><code>true</code> when the document is in {{DOMxRef("Using_full-screen_mode","full-screen mode")}}.</dd> <dt>{{DOMxRef("Document.height")}} {{Non-standard_Inline}} {{Obsolete_Inline}}</dt> <dd>获取或设置 the height of 当前文档。</dd> <dt>{{DOMxRef("Document.inputEncoding")}} {{ReadOnlyInline}} {{Deprecated_Inline}}</dt> <dd>Alias of {{DOMxRef("Document.characterSet")}}. Use this property instead.</dd> <dt>{{DOMxRef("Document.linkColor")}} {{Deprecated_Inline}}</dt> <dd>获取或设置 the color of hyperlinks in the document.</dd> <dt>{{DOMxRef("Document.vlinkColor")}} {{Deprecated_Inline}}</dt> <dd>获取或设置 the color of visited hyperlinks.</dd> <dt>{{DOMxRef("Document.width")}} {{Non-standard_Inline}} {{Obsolete_Inline}}</dt> <dd>返回 width of 当前文档。</dd> <dt>{{DOMxRef("Document.xmlEncoding")}} {{Deprecated_Inline}}</dt> <dd>返回 encoding as determined by the XML declaration.</dd> <dt>{{DOMxRef("Document.xmlStandalone")}} {{Obsolete_Inline("10.0")}}</dt> <dd>Returns <code>true</code> if the XML declaration specifies the document to be standalone (<em>e.g.,</em> An external part of the DTD affects the document's content), else <code>false</code>.</dd> <dt>{{DOMxRef("Document.xmlVersion")}} {{Obsolete_Inline("10.0")}}</dt> <dd>返回 version number as specified in the XML declaration or <code>"1.0"</code> if the declaration is absent.</dd> </dl> <h2 id="Methods">Methods</h2> <p><em>该接口同样继承了 {{DOMxRef("Node")}} 和 {{DOMxRef("EventTarget")}} 接口。</em></p> <dl> <dt>{{DOMxRef("Document.adoptNode()")}}</dt> <dd>从外部文档中采用节点。</dd> <dt>{{DOMxRef("Document.captureEvents()")}} {{Deprecated_Inline}}</dt> <dd>参见 {{DOMxRef("Window.captureEvents")}}。</dd> <dt>{{DOMxRef("Document.caretRangeFromPoint()")}} {{Non-standard_Inline}}</dt> <dd>Gets a {{DOMxRef("Range")}} object for the document fragment under the specified coordinates.</dd> <dt>{{DOMxRef("Document.createAttribute()")}}</dt> <dd>创建一个新的 {{DOMxRef("Attr")}} 对象并返回。</dd> <dt>{{DOMxRef("Document.createAttributeNS()")}}</dt> <dd>在给定命名空间创建一个新的属性节点并返回。</dd> <dt>{{DOMxRef("Document.createCDATASection()")}}</dt> <dd>创建一个新的数据节点(CDATA node)并返回。</dd> <dt>{{DOMxRef("Document.createComment()")}}</dt> <dd>创建一个新的注释节点并返回。</dd> <dt>{{DOMxRef("Document.createDocumentFragment()")}}</dt> <dd>创建一个新的文档片段。</dd> <dt>{{DOMxRef("Document.createElement()")}}</dt> <dd>用给定标签名 tagName 创建一个新的元素。</dd> <dt>{{DOMxRef("Document.createElementNS()")}}</dt> <dd>用给定标签名 tagName 和命名空间创建一个新的元素。</dd> <dt>{{DOMxRef("Document.createEntityReference()")}} {{Obsolete_Inline}}</dt> <dd>创建一个 new entity reference object and returns it.</dd> <dt>{{DOMxRef("Document.createEvent()")}}</dt> <dd>创建一个 event 对象。</dd> <dt>{{DOMxRef("Document.createNodeIterator()")}}</dt> <dd>创建一个 {{DOMxRef("NodeIterator")}} 对象。</dd> <dt>{{DOMxRef("Document.createProcessingInstruction()")}}</dt> <dd>创建一个 new {{DOMxRef("ProcessingInstruction")}} 对象。</dd> <dt>{{DOMxRef("Document.createRange()")}}</dt> <dd>创建一个 {{DOMxRef("Range")}} 对象。</dd> <dt>{{DOMxRef("Document.createTextNode()")}}</dt> <dd>创建一个 text node.</dd> <dt>{{DOMxRef("Document.createTouch()")}} {{Deprecated_Inline}}</dt> <dd>创建一个 {{DOMxRef("Touch")}} 对象。</dd> <dt>{{DOMxRef("Document.createTouchList()")}}</dt> <dd>创建一个 {{DOMxRef("TouchList")}} 对象。</dd> <dt>{{DOMxRef("Document.createTreeWalker()")}}</dt> <dd>创建一个 {{DOMxRef("TreeWalker")}} 对象。</dd> <dt>{{DOMxRef("Document.enableStyleSheetsForSet()")}}</dt> <dd>Enables the style sheets for the specified style sheet set.</dd> <dt>{{DOMxRef("Document.exitPointerLock()")}} {{Experimental_Inline}}</dt> <dd>Release the pointer lock.</dd> <dt>{{DOMxRef("Document.getAnimations()")}} {{Experimental_Inline}}</dt> <dd>Returns an array of all {{DOMxRef("Animation")}} objects currently in effect, whose target elements are descendants of the <code>document</code>.</dd> <dt>{{DOMxRef("Document.getElementsByClassName()")}}</dt> <dd>Returns a list of elements with the given class name.</dd> <dt>{{DOMxRef("Document.getElementsByTagName()")}}</dt> <dd>Returns a list of elements with the given tag name.</dd> <dt>{{DOMxRef("Document.getElementsByTagNameNS()")}}</dt> <dd>Returns a list of elements with the given tag name and namespace.</dd> <dt>{{DOMxRef("Document.hasStorageAccess()")}}</dt> <dd>Returns a {{jsxref("Promise")}} that resolves with a boolean value indicating whether the document has access to its first-party storage.</dd> <dt>{{DOMxRef("Document.importNode()")}}</dt> <dd>Returns a clone of a node from an external document.</dd> <dt>{{DOMxRef("Document.normalizeDocument()")}} {{Obsolete_Inline}}</dt> <dd>Replaces entities, normalizes text nodes, etc.</dd> <dt>{{DOMxRef("Document.releaseCapture()")}} {{Non-standard_Inline}} {{gecko_minversion_inline("2.0")}}</dt> <dd>Releases the current mouse capture if it's on an element in this document.</dd> <dt>{{DOMxRef("Document.releaseEvents()")}} {{Non-standard_Inline}} {{Deprecated_Inline}}</dt> <dd>详见 {{DOMxRef("Window.releaseEvents()")}}。</dd> <dt>{{DOMxRef("Document.requestStorageAccess()")}}</dt> <dd>Returns a {{jsxref("Promise")}} that resolves if the access to first-party storage was granted, and rejects if access was denied.</dd> <dt>{{DOMxRef("Document.routeEvent()")}} {{Non-standard_Inline}} {{Obsolete_Inline(24)}}</dt> <dd>See {{DOMxRef("Window.routeEvent()")}}.</dd> <dt>{{DOMxRef("Document.mozSetImageElement()")}} {{Non-standard_Inline}}</dt> <dd>Allows you to change the element being used as the background image for a specified element ID.</dd> </dl> <p>The <code>Document</code> interface is extended with the {{DOMxRef("ParentNode")}} interface:</p> <dl> <dt>{{DOMxRef("document.getElementById","document.getElementById(String id)")}}</dt> <dd>Returns an object reference to the identified element.</dd> <dt>{{DOMxRef("document.querySelector()")}}</dt> <dd>返回 first Element node within the document, in document order, that matches the specified selectors.</dd> <dt>{{DOMxRef("document.querySelectorAll()")}}</dt> <dd>Returns a list of all the Element nodes within the document that match the specified selectors.</dd> </dl> <p>The <code>Document</code> interface is extended with the {{DOMxRef("XPathEvaluator")}} interface:</p> <dl> <dt>{{DOMxRef("document.createExpression()")}}</dt> <dd>Compiles an <code><a href="/en-US/docs/XPathExpression" title="XPathExpression">XPathExpression</a></code> which can then be used for (repeated) evaluations.</dd> <dt>{{DOMxRef("document.createNSResolver()")}}</dt> <dd>创建一个 {{DOMxRef("XPathNSResolver")}} 对象。</dd> <dt>{{DOMxRef("document.evaluate()")}}</dt> <dd>Evaluates an XPath expression.</dd> </dl> <h3 id="Extension_for_HTML_documents">Extension for HTML documents</h3> <p>The <code>Document</code> interface for HTML documents inherit from the {{DOMxRef("HTMLDocument")}} interface or, since HTML5, is extended for such documents:</p> <dl> <dt>{{DOMxRef("document.clear()")}} {{Non-standard_Inline}} {{Deprecated_Inline}}</dt> <dd>In majority of modern browsers, including recent versions of Firefox and Internet Explorer, this method does nothing.</dd> <dt>{{DOMxRef("document.close()")}}</dt> <dd>Closes a document stream for writing.</dd> <dt>{{DOMxRef("document.execCommand()")}}</dt> <dd>On an editable document, executes a formating command.</dd> <dt>{{DOMxRef("document.getElementsByName()")}}</dt> <dd>Returns a list of elements with the given name.</dd> <dt>{{DOMxRef("document.hasFocus()")}}</dt> <dd>Returns <code>true</code> if the focus is currently located anywhere inside the specified document.</dd> <dt>{{DOMxRef("document.open()")}}</dt> <dd>Opens a document stream for writing.</dd> <dt>{{DOMxRef("document.queryCommandEnabled()")}}</dt> <dd>Returns true if the formating command can be executed on the current range.</dd> <dt>{{DOMxRef("document.queryCommandIndeterm()")}}</dt> <dd>Returns true if the formating command is in an indeterminate state on the current range.</dd> <dt>{{DOMxRef("document.queryCommandState()")}}</dt> <dd>Returns true if the formating command has been executed on the current range.</dd> <dt>{{DOMxRef("document.queryCommandSupported()")}}</dt> <dd>Returns true if the formating command is supported on the current range.</dd> <dt>{{DOMxRef("document.queryCommandValue()")}}</dt> <dd>返回 current value of the current range for a formating command.</dd> <dt>{{DOMxRef("document.write()")}}</dt> <dd>Writes text in a document.</dd> <dt>{{DOMxRef("document.writeln()")}}</dt> <dd>Writes a line of text in a document.</dd> </dl> <h3 id="Methods_included_from_DocumentOrShadowRoot">Methods included from DocumentOrShadowRoot</h3> <p><em>The <code>Document</code> interface includes the following methods defined on the {{DOMxRef("DocumentOrShadowRoot")}} mixin. Note that this is currently only implemented by Chrome; other browsers still implement them on the {{DOMxRef("Document")}} interface.</em></p> <dl> <dt>{{DOMxRef("DocumentOrShadowRoot.getSelection()")}}</dt> <dd>Returns a {{DOMxRef('Selection')}} object representing the range of text selected by the user, or the current position of the caret.</dd> <dt>{{DOMxRef("DocumentOrShadowRoot.elementFromPoint()")}}</dt> <dd>返回 topmost element at the specified coordinates.</dd> <dt>{{DOMxRef("DocumentOrShadowRoot.elementsFromPoint()")}}</dt> <dd>Returns an array of all elements at the specified coordinates.</dd> <dt>{{DOMxRef("DocumentOrShadowRoot.caretPositionFromPoint()")}}</dt> <dd>Returns a {{DOMxRef('CaretPosition')}} object containing the DOM node containing the caret, and caret's character offset within that node.</dd> </dl> <h2 id="Events">Events</h2> <p>Listen to these events using <code>addEventListener()</code> or by assigning an event listener to the <code>on<em>eventname</em></code> property of this interface.</p> <dl> <dt>{{DOMxRef("Document/scroll_event", "scroll")}}</dt> <dd>Fired when the document view or an element has been scrolled.<br> Also available via the {{DOMxRef("GlobalEventHandlers.onscroll", "onscroll")}} 属性。</dd> <dt>{{DOMxRef("Document/visibilitychange_event", "visibilitychange")}}</dt> <dd>Fired when the content of a tab has become visible or has been hidden.<br> Also available via the {{DOMxRef("Document.onvisibilitychange", "onvisibilitychange")}} 属性。</dd> <dt>{{DOMxRef("Document/wheel_event","wheel")}}</dt> <dd>Fired when the user rotates a wheel button on a pointing device (typically a mouse).<br> Also available via the {{DOMxRef("GlobalEventHandlers.onwheel", "onwheel")}} 属性。</dd> </dl> <h3 id="Animation_events">Animation events</h3> <dl> <dt>{{domxref("Document/animationcancel_event", "animationcancel")}}</dt> <dd>Fired when an animation unexpectedly aborts.<br> Also available via the {{domxref("GlobalEventHandlers/onanimationcancel", "onanimationcancel")}} 属性。</dd> <dt>{{domxref("Document/animationend_event", "animationend")}}</dt> <dd>Fired when an animation has completed normally.<br> Also available via the {{domxref("GlobalEventHandlers/onanimationend", "onanimationend")}} 属性。</dd> <dt>{{domxref("Document/animationiteration_event", "animationiteration")}}</dt> <dd>Fired when an animation iteration has completed.<br> Also available via the {{domxref("GlobalEventHandlers/onanimationiteration", "onanimationiteration")}} 属性。</dd> <dt>{{domxref("Document/animationstart_event", "animationstart")}}</dt> <dd>Fired when an animation starts.<br> Also available via the {{domxref("GlobalEventHandlers/onanimationstart", "onanimationstart")}} 属性。</dd> </dl> <h3 id="Clipboard_events">Clipboard events</h3> <dl> <dt>{{domxref("Document/copy_event", "copy")}}</dt> <dd>Fired when the user initiates a copy action through the browser's user interface.<br> Also available via the {{domxref("HTMLElement/oncopy", "oncopy")}} 属性。</dd> <dt>{{domxref("Document/cut_event", "cut")}}</dt> <dd>Fired when the user initiates a cut action through the browser's user interface.<br> Also available via the {{domxref("HTMLElement/oncut", "oncut")}} 属性。</dd> <dt>{{domxref("Document/paste_event", "paste")}}</dt> <dd>Fired when the user initiates a paste action through the browser's user interface.<br> Also available via the {{domxref("HTMLElement/onpaste", "onpaste")}} 属性。</dd> </dl> <h3 id="Drag_drop_events">Drag & drop events</h3> <dl> <dt>{{domxref("Document/drag_event", "drag")}}</dt> <dd>Fired every few hundred milliseconds as an element or text selection is being dragged by the user.<br> Also available via the {{domxref("GlobalEventHandlers/ondrag", "ondrag")}} 属性。</dd> <dt>{{domxref("Document/dragend_event", "dragend")}}</dt> <dd>Fired when a drag operation is being ended (by releasing a mouse button or hitting the escape key).<br> Also available via the {{domxref("GlobalEventHandlers/ondragend", "ondragend")}} 属性。</dd> <dt>{{domxref("Document/dragenter_event", "dragenter")}}</dt> <dd>Fired when a dragged element or text selection enters a valid drop target.<br> Also available via the {{domxref("GlobalEventHandlers/ondragenter", "ondragenter")}} 属性。</dd> <dt>{{domxref("Document/dragexit_event", "dragexit")}}</dt> <dd>Fired when an element is no longer the drag operation's immediate selection target.<br> Also available via the {{domxref("GlobalEventHandlers/ondragexit", "ondragexit")}} 属性。</dd> <dt>{{domxref("Document/dragleave_event", "dragleave")}}</dt> <dd>Fired when a dragged element or text selection leaves a valid drop target.<br> Also available via the {{domxref("GlobalEventHandlers/ondragleave", "ondragleave")}} 属性。</dd> <dt>{{domxref("Document/dragover_event", "dragover")}}</dt> <dd>Fired when an element or text selection is being dragged over a valid drop target (every few hundred milliseconds).<br> Also available via the {{domxref("GlobalEventHandlers/ondragover", "ondragover")}} 属性。</dd> <dt>{{domxref("Document/dragstart_event", "dragstart")}}</dt> <dd>Fired when the user starts dragging an element or text selection.<br> Also available via the {{domxref("GlobalEventHandlers/ondragstart", "ondragstart")}} 属性。</dd> <dt>{{domxref("Document/drop_event", "drop")}}</dt> <dd>Fired when an element or text selection is dropped on a valid drop target.<br> Also available via the {{domxref("GlobalEventHandlers/ondrop", "ondrop")}} 属性。</dd> </dl> <h3 id="Fullscreen_events">Fullscreen events</h3> <dl> <dt>{{domxref("Document/fullscreenchange_event", "fullscreenchange")}}</dt> <dd>Fired when the <code>Document</code> transitions into or out of <a href="/en-US/docs/Web/API/Fullscreen_API/Guide">full-screen</a> mode.<br> Also available via the <code> {{DOMxRef("Document.onfullscreenchange", "onfullscreenchange")}}</code> 属性。</dd> <dt><code><a href="/en-US/docs/Web/Events/fullscreenerror">fullscreenerror</a></code></dt> <dd>Fired if an error occurs while attempting to switch into or out of <a href="/en-US/docs/Web/API/Fullscreen_API/Guide">full-screen</a> mode.<br> Also available via the {{DOMxRef("Document.onfullscreenerror", "onfullscreenerror")}} 属性。</dd> <dt> <h3 id="Keyboard_events">Keyboard events</h3> </dt> <dt><code>{{DOMxRef("Document/keydown_event", "keydown")}}</code></dt> <dd>Fired when a key is pressed.<br> Also available via the {{DOMxRef("GlobalEventHandlers/onkeydown", "onkeydown")}} 属性。</dd> <dt><code>{{DOMxRef("Document/keypress_event", "keypress")}}</code></dt> <dd>Fired when a key that produces a character value is pressed down. {{Deprecated_Inline}}<br> Also available via the {{DOMxRef("GlobalEventHandlers/onkeypress", "onkeypress")}} 属性。</dd> <dt><code>{{DOMxRef("Document/keyup_event", "keyup")}}</code></dt> <dd>Fired when a key is released.<br> Also available via the {{DOMxRef("GlobalEventHandlers/onkeyup", "onkeyup")}} 属性。</dd> </dl> <h3 id="Load_unload_events">Load & unload events</h3> <dl> <dt>{{domxref("Document/DOMContentLoaded_event", "DOMContentLoaded")}}</dt> <dd>Fired when the document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.</dd> <dt>{{domxref("Document/readystatechange_event", "readystatechange")}}</dt> <dd>Fired when the {{domxref("Document/readyState", "readyState")}} attribute of a document has changed.<br> Also available via the <code>onreadystatechange</code> 属性。</dd> </dl> <h3 id="Pointer_events">Pointer events</h3> <dl> <dt>{{domxref("Document/gotpointercapture_event", "gotpointercapture")}}</dt> <dd>Fired when when an element captures a pointer using <code><a href="/en-US/docs/Web/API/Element/setPointerCapture">setPointerCapture()</a></code>.<br> Also available via the {{domxref("GlobalEventHandlers/ongotpointercapture", "ongotpointercapture")}} 属性。</dd> <dt>{{domxref("Document/lostpointercapture_event", "lostpointercapture")}}</dt> <dd>Fired when a <a href="/en-US/docs/Web/API/Pointer_events#Pointer_capture">captured pointer</a> is released.<br> Also available via the {{domxref("GlobalEventHandlers/onlostpointercapture", "onlostpointercapture")}} 属性。</dd> <dt>{{domxref("Document/pointercancel_event", "pointercancel")}}</dt> <dd>Fired when a pointer event is canceled.<br> Also available via the {{domxref("GlobalEventHandlers/onpointercancel", "onpointercancel")}} 属性。</dd> <dt>{{domxref("Document/pointerdown_event", "pointerdown")}}</dt> <dd>Fired when a pointer becomes active.<br> Also available via the {{domxref("GlobalEventHandlers/onpointerdown", "onpointerdown")}} 属性。</dd> <dt>{{domxref("Document/pointerenter_event", "pointerenter")}}</dt> <dd>Fired when a pointer is moved into the hit test boundaries of an element or one of its descendants.<br> Also available via the {{domxref("GlobalEventHandlers/onpointerenter", "onpointerenter")}} 属性。</dd> <dt>{{domxref("Document/pointerleave_event", "pointerleave")}}</dt> <dd>Fired when a pointer is moved out of the hit test boundaries of an element.<br> Also available via the {{domxref("GlobalEventHandlers/onpointerleave", "onpointerleave")}} 属性。</dd> <dt>{{domxref("Document/pointerlockchange_event", "pointerlockchange")}}</dt> <dd>Fired when the pointer is locked/unlocked.<br> Also available via the {{domxref("GlobalEventHandlers/onpointerlockchange", "onpointerlockchange")}} 属性。</dd> <dt>{{domxref("Document/pointerlockerror_event", "pointerlockerror")}}</dt> <dd>Fired when locking the pointer failed.<br> Also available via the {{domxref("GlobalEventHandlers/onpointerlockerror", "onpointerlockerror")}} 属性。</dd> <dt>{{domxref("Document/pointermove_event", "pointermove")}}</dt> <dd>Fired when a pointer changes coordinates.<br> Also available via the {{domxref("GlobalEventHandlers/onpointermove", "onpointermove")}} 属性。</dd> <dt>{{domxref("Document/pointerout_event", "pointerout")}}</dt> <dd>Fired when a pointer is moved out of the <em>hit test</em> boundaries of an element (among other reasons).<br> Also available via the {{domxref("GlobalEventHandlers/onpointerout", "onpointerout")}} 属性。</dd> <dt>{{domxref("Document/pointerover_event", "pointerover")}}</dt> <dd>Fired when a pointer is moved into an element's hit test boundaries.<br> Also available via the {{domxref("GlobalEventHandlers/onpointerover", "onpointerover")}} 属性。</dd> <dt>{{domxref("Document/pointerup_event", "pointerup")}}</dt> <dd>Fired when a pointer is no longer active.<br> Also available via the {{domxref("GlobalEventHandlers/onpointerup", "onpointerup")}} 属性。</dd> </dl> <h3 id="Selection_events">Selection events</h3> <dl> <dt><code>{{DOMxRef("Document/selectionchange_event", "selectionchange")}}</code></dt> <dd>Fired when the current text selection on a document is changed.<br> Also available via the {{DOMxRef("GlobalEventHandlers/onselectionchange", "onselectionchange")}} 属性。</dd> <dt>{{DOMxRef("Document/selectstart_event", "selectstart")}}</dt> <dd>Fired when the user begins a new selection.<br> Also available via the {{DOMxRef("GlobalEventHandlers/onselectstart", "onselectstart")}} 属性。</dd> </dl> <h3 id="Touch_events">Touch events</h3> <dl> <dt>{{domxref("Document/touchcancel_event", "touchcancel")}}</dt> <dd>Fired when one or more touch points have been disrupted in an implementation-specific manner (for example, too many touch points are created).<br> Also available via the {{domxref("GlobalEventHandlers/ontouchcancel", "ontouchcancel")}} 属性。</dd> <dt>{{domxref("Document/touchend_event", "touchend")}}</dt> <dd>Fired when one or more touch points are removed from the touch surface.<br> Also available via the {{domxref("GlobalEventHandlers/ontouchend", "ontouchend")}} property</dd> <dt>{{domxref("Document/touchmove_event", "touchmove")}}</dt> <dd>Fired when one or more touch points are moved along the touch surface.<br> Also available via the {{domxref("GlobalEventHandlers/ontouchmove", "ontouchmove")}} property</dd> <dt>{{domxref("Document/touchstart_event", "touchstart")}}</dt> <dd>Fired when one or more touch points are placed on the touch surface.<br> Also available via the {{domxref("GlobalEventHandlers/ontouchstart", "ontouchstart")}} property</dd> <dt> <h3 id="Transition_events">Transition events</h3> </dt> <dt>{{domxref("Document/transitioncancel_event", "transitioncancel")}}</dt> <dd>Fired when a <a href="/en-US/docs/CSS/Using_CSS_transitions">CSS transition</a> is canceled.<br> Also available via the {{domxref("GlobalEventHandlers/ontransitioncancel", "ontransitioncancel")}} 属性。</dd> <dt>{{domxref("Document/transitionend_event", "transitionend")}}</dt> <dd>Fired when a <a href="/en-US/docs/CSS/Using_CSS_transitions">CSS transition</a> has completed.<br> Also available via the {{domxref("GlobalEventHandlers/ontransitionend", "ontransitionend")}} 属性。</dd> <dt>{{domxref("Document/transitionrun_event", "transitionrun")}}</dt> <dd>Fired when a <a href="/en-US/docs/CSS/Using_CSS_transitions">CSS transition</a> is first created.<br> Also available via the {{domxref("GlobalEventHandlers/ontransitionrun", "ontransitionrun")}} 属性。</dd> <dt>{{domxref("Document/transitionstart_event", "transitionstart")}}</dt> <dd>Fired when a <a href="/en-US/docs/CSS/Using_CSS_transitions">CSS transition</a> has actually started.<br> Also available via the {{domxref("GlobalEventHandlers/ontransitionstart", "ontransitionstart")}} 属性。</dd> </dl> <h2 id="Non-standard_extensions_Non-standard_Inline">Non-standard extensions {{Non-standard_Inline}}</h2> <div>{{non-standard_header}}</div> <h3 id="Firefox_notes">Firefox notes</h3> <p>Mozilla defines a set of non-standard properties made only for XUL content:</p> <dl> <dt>{{DOMxRef("document.currentScript")}} {{Non-standard_Inline}}</dt> <dd>返回 {{HTMLElement("script")}} element that is currently executing.</dd> <dt>{{DOMxRef("document.documentURIObject")}}</dt> <dd>(<strong>Mozilla add-ons only!</strong>) 返回 {{Interface("nsIURI")}} object representing the URI of the document. This property only has special meaning in privileged JavaScript code (with UniversalXPConnect privileges).</dd> <dt>{{DOMxRef("document.popupNode")}}</dt> <dd>返回 node upon which a popup was invoked.</dd> <dt>{{DOMxRef("document.tooltipNode")}}</dt> <dd>返回 node which is the target of the current tooltip.</dd> </dl> <p>Mozilla also define some non-standard methods:</p> <dl> <dt>{{DOMxRef("document.execCommandShowHelp")}} {{Obsolete_Inline("14.0")}}</dt> <dd>This method never did anything and always threw an exception, so it was removed in Gecko 14.0 {{geckoRelease("14.0")}}.</dd> <dt>{{DOMxRef("document.getBoxObjectFor")}} {{Obsolete_Inline}}</dt> <dd>Use the {{DOMxRef("Element.getBoundingClientRect()")}} method instead.</dd> <dt>{{DOMxRef("document.loadOverlay")}}</dt> <dd>Loads a <a href="/en-US/docs/XUL_Overlays" title="XUL_Overlays">XUL overlay</a> dynamically. This only works in XUL documents.</dd> <dt>{{DOMxRef("document.queryCommandText")}} {{Obsolete_Inline("14.0")}}</dt> <dd>This method never did anything but throw an exception, and was removed in Gecko 14.0 {{geckoRelease("14.0")}}.</dd> </dl> <h3 id="Internet_Explorer_notes">Internet Explorer notes</h3> <p>Microsoft defines some non-standard properties:</p> <dl> <dt>{{DOMxRef("document.fileSize")}}* {{Non-standard_Inline}} {{Obsolete_Inline}}</dt> <dd>Returns size in bytes of the document. Starting with Internet Explorer 11, that property is no longer supported. See <a href="http://msdn.microsoft.com/en-us/library/ms533752%28v=VS.85%29.aspx">MSDN</a>.</dd> </dl> <p>Internet Explorer does not support all methods from the <code>Node</code> interface in the <code>Document</code> interface:</p> <dl> <dt>{{DOMxRef("document.contains")}}</dt> <dd>As a work-around, <code>document.body.contains()</code> can be used.</dd> </dl> <h2 id="规范">规范</h2> <table class="standard-table"> <thead> <tr> <th scope="col">规范</th> <th scope="col">状态</th> <th scope="col">备注</th> </tr> </thead> <tbody> <tr> <td>{{SpecName("DOM WHATWG", "#interface-document", "Document")}}</td> <td>{{Spec2("DOM WHATWG")}}</td> <td>Intend to supersede DOM 3</td> </tr> <tr> <td>{{SpecName("HTML WHATWG", "dom.html#the-document-object", "Document")}}</td> <td>{{Spec2("HTML WHATWG")}}</td> <td>Turn the {{DOMxRef("HTMLDocument")}} interface into a <code>Document</code> extension.</td> </tr> <tr> <td>{{SpecName("HTML Editing", "#dom-document-getselection", "Document")}}</td> <td>{{Spec2("HTML Editing")}}</td> <td>Extend the <code>Document</code> interface</td> </tr> <tr> <td>{{SpecName("CSSOM View", "#extensions-to-the-document-interface", "Document")}}</td> <td>{{Spec2("CSSOM View")}}</td> <td>Extend the <code>Document</code> interface</td> </tr> <tr> <td>{{SpecName("CSSOM", "#extensions-to-the-document-interface", "Document")}}</td> <td>{{Spec2("CSSOM")}}</td> <td>Extend the <code>Document</code> interface</td> </tr> <tr> <td>{{SpecName("Pointer Lock", "#extensions-to-the-document-interface", "Document")}}</td> <td>{{Spec2("Pointer Lock")}}</td> <td>Extend the <code>Document</code> interface</td> </tr> <tr> <td>{{SpecName("Page Visibility API", "#extensions-to-the-document-interface", "Document")}}</td> <td>{{Spec2("Page Visibility API")}}</td> <td>Extend the <code>Document</code> interface with the <code>visibilityState</code> and <code>hidden</code> attributes and the <code>onvisibilitychange</code> event listener.</td> </tr> <tr> <td>{{SpecName("Selection API", "#extensions-to-document-interface", "Document")}}</td> <td>{{Spec2("Selection API")}}</td> <td>Adds <code>getSelection()</code>, <code>onselectstart</code> and <code>onselectionchange</code>.</td> </tr> <tr> <td>{{SpecName("DOM4", "#interface-document", "Document")}}</td> <td>{{Spec2("DOM4")}}</td> <td>Supersede DOM 3</td> </tr> <tr> <td>{{SpecName("DOM3 Core", "#i-Document", "Document")}}</td> <td>{{Spec2("DOM3 Core")}}</td> <td>Supersede DOM 2</td> </tr> <tr> <td>{{SpecName("DOM3 XPath", "xpath.html#XPathEvaluator", "XPathEvaluator")}}</td> <td>{{Spec2("DOM3 XPath")}}</td> <td>Define the {{DOMxRef("XPathEvaluator")}} interface which extend document.</td> </tr> <tr> <td>{{SpecName("DOM2 Core", "#i-Document", "Document")}}</td> <td>{{Spec2("DOM2 Core")}}</td> <td>Supersede DOM 1</td> </tr> <tr> <td>{{SpecName("DOM1", "#i-Document", "Document")}}</td> <td>{{Spec2("DOM1")}}</td> <td>Initial definition for the interface</td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> <p>{{Compat("api.Document")}}</p>