此页面上有脚本错误。虽然它是写给网站编辑,您可以在下面查看部分内容。

翻译正在进行中。

{{APIRef}}

The Document interface represents any web page loaded in the browser and serves as an entry point into the web page's content, which is the DOM tree. The DOM tree includes elements such as {{HTMLElement("body")}} and {{HTMLElement("table")}}, among many others. It provides functionality globally to the document, like how to obtain the page's URL and create new elements in the document.

{{inheritanceDiagram}}

The Document interface describes the common properties and methods for any kind of document. Depending on the document's type (e.g. HTML, XML, SVG, …), a larger API is available: HTML documents, served with the "text/html" content type, also implement the {{domxref("HTMLDocument")}} interface, whereas XML and SVG documents implement the {{domxref("XMLDocument")}} interface.

Constructor

{{domxref("Document.Document","Document()")}}{{non-standard_inline}}
Creates a new Document object.

Properties

This interface also inherits from the {{domxref("Node")}} and {{domxref("EventTarget")}} interfaces.

{{domxref("Document.anchors")}} {{readonlyinline}}
Returns a list of all of the anchors in the document.
{{domxref("Document.body")}}
Returns the {{HTMLElement("body")}} or {{htmlelement("frameset")}} node of the current document.
{{domxref("Document.characterSet")}} {{readonlyinline}}
Returns the character set being used by the document.
{{domxref("Document.compatMode")}} {{readonlyinline}} {{experimental_inline}}
Indicates whether the document is rendered in quirks or strict mode.
{{domxref("Document.contentType")}} {{readonlyinline}} {{experimental_inline}}
Returns the Content-Type from the MIME Header of the current document.
{{domxref("Document.doctype")}} {{readonlyinline}}
Returns the Document Type Definition (DTD) of the current document.
{{domxref("Document.documentElement")}} {{readonlyinline}}
Returns the {{domxref("Element")}} that is a direct child of the document. For HTML documents, this is normally the {{domxref("HTMLElement")}} element.
{{domxref("Document.documentURI")}} {{readonlyinline}}
Returns the document location as a string.
{{domxref("Document.embeds")}} {{readonlyinline}}
Returns a list of the embedded {{HTMLElement('embed')}} elements within the current document.
{{domxref("Document.forms")}} {{readonlyinline}}
Returns a list of the {{HTMLElement("form")}} elements within the current document.
{{domxref("Document.head")}} {{readonlyinline}}
Returns the {{HTMLElement("head")}} element of the current document.
{{domxref("Document.hidden")}} {{readonlyinline}}
{{domxref("Document.images")}} {{readonlyinline}}
Returns a list of the images in the current document.
{{domxref("Document.implementation")}} {{readonlyinline}}
Returns the DOM implementation associated with the current document.
{{domxref("Document.lastStyleSheetSet")}} {{readonlyinline}}
Returns the name of the style sheet set that was last enabled. Has the value null until the style sheet is changed by setting the value of {{domxref("document.selectedStyleSheetSet","selectedStyleSheetSet")}}.
{{domxref("Document.links")}} {{readonlyinline}}
Returns a list of all the hyperlinks in the document.
{{domxref("Document.mozSyntheticDocument")}} {{non-standard_inline}} {{gecko_minversion_inline("8.0")}}
Returns a {{jsxref("Boolean")}} that is true only if this document is synthetic, such as a standalone image, video, audio file, or the like.
{{domxref("Document.mozFullScreenElement")}} {{readonlyinline}} {{non-standard_inline}} {{gecko_minversion_inline("9.0")}}
The element that's currently in full screen mode for this document.
{{domxref("Document.mozFullScreenEnabled")}} {{readonlyinline}} {{non-standard_inline}} {{gecko_minversion_inline("9.0")}}
true if calling {{domxref("Element.mozRequestFullscreen()")}} would succeed in the curent document.
{{domxref("Document.plugins")}} {{readonlyinline}}
Returns a list of the available plugins.
{{domxref("Document.preferredStyleSheetSet")}} {{readonlyinline}}
Returns the preferred style sheet set as specified by the page author.
{{domxref("Document.scripts")}} {{readonlyinline}}
Returns all the {{HTMLElement("script")}} elements on the document.
{{domxref("Document.scrollingElement")}} {{readonlyinline}}
Returns a reference to the {{domxref("Element")}} that scrolls the document.
{{domxref("Document.selectedStyleSheetSet")}}
Returns which style sheet set is currently in use.
{{domxref("Document.styleSheetSets")}} {{readonlyinline}}
Returns a list of the style sheet sets available on the document.
{{domxref("Document.timeline")}} {{readonlyinline}}
{{domxref("Document.undoManager")}} {{readonlyinline}} {{experimental_inline}}
{{domxref("Document.visibilityState")}} {{readonlyinline}}

Returns a string denoting the visibility state of the document. Possible values are visiblehiddenprerender, and unloaded.

The Document interface is extended with the {{domxref("ParentNode")}} interface:

{{page("/en-US/docs/Web/API/ParentNode","Properties")}}

Extensions for HTMLDocument

The Document interface for HTML documents inherits from the {{domxref("HTMLDocument")}} interface or, since HTML5,  is extended for such documents.

{{domxref("Document.cookie")}}
Returns a semicolon-separated list of the cookies for that document or sets a single cookie.
{{domxref("Document.defaultView")}} {{readonlyinline}}
Returns a reference to the window object.
{{domxref("Document.designMode")}}
Gets/sets the ability to edit the whole document.
{{domxref("Document.dir")}} {{readonlyinline}}
Gets/sets directionality (rtl/ltr) of the document.
{{domxref("Document.domain")}}
Gets/sets the domain of the current document.
{{domxref("Document.lastModified")}} {{readonlyinline}}
Returns the date on which the document was last modified.
{{domxref("Document.location")}} {{readonlyinline}}
Returns the URI of the current document.
{{domxref("Document.readyState")}} {{readonlyinline}}  {{gecko_minversion_inline("1.9.2")}}
Returns loading status of the document.
{{domxref("Document.referrer")}} {{readonlyinline}}
Returns the URI of the page that linked to this page.
{{domxref("Document.title")}}
Sets or gets the title of the current document.
{{domxref("Document.URL")}} {{readonlyInline}}
Returns the document location as a string.

Properties included from DocumentOrShadowRoot

The Document interface includes the following properties defined on the {{domxref("DocumentOrShadowRoot")}} mixin. Note that this is currently only implemented by Chrome; other browsers still implement them directly on the {{domxref("Document")}} interface.

{{domxref("DocumentOrShadowRoot.activeElement")}} {{readonlyInline}}
Returns the {{domxref('Element')}} within the shadow tree that has focus.
{{domxref("Document.fullscreenElement")}} {{readonlyinline}}
The element that's currently in full screen mode for this document.
{{domxref("DocumentOrShadowRoot.pointerLockElement")}} {{readonlyinline}} {{experimental_inline}}
Returns the element set as the target for mouse events while the pointer is locked. null if lock is pending, pointer is unlocked, or if the target is in another document.
{{domxref("DocumentOrShadowRoot.styleSheets")}} {{readonlyInline}}
Returns a {{domxref('StyleSheetList')}} of {{domxref('CSSStyleSheet')}} objects for stylesheets explicitly linked into, or embedded in a document.

Event handlers

{{domxref("Document.onafterscriptexecute")}} {{non-standard_inline}}
Represents the event handling code for the {{event("afterscriptexecute")}} event.
{{domxref("Document.onbeforescriptexecute")}} {{non-standard_inline}}
Represents the event handling code for the {{event("beforescriptexecute")}} event.
{{domxref("Document.oncopy")}} {{non-standard_inline}}
Represents the event handling code for the {{event("copy")}} event.
{{domxref("Document.oncut")}} {{non-standard_inline}}
Represents the event handling code for the {{event("cut")}} event.
{{domxref("Document.onfullscreenchange")}}
Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("fullscreenchange")}} event is raised.
{{domxref("Document.onfullscreenerror")}}
Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("fullscreenerror")}} event is raised.
{{domxref("Document.onpaste")}} {{non-standard_inline}}
Represents the event handling code for the {{event("paste")}} event.
{{domxref("Document.onpointerlockchange")}} {{experimental_inline}}
Represents the event handling code for the {{event("pointerlockchange")}} event.
{{domxref("Document.onpointerlockerror")}} {{experimental_inline}}
Represents the event handling code for the {{event("pointerlockerror")}} event.
{{domxref("Document.onreadystatechange")}} {{gecko_minversion_inline("1.9.2")}}
Represents the event handling code for the {{event("readystatechange")}} event.
{{domxref("Document.onselectionchange")}} {{experimental_inline}}
Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("selectionchange")}} event is raised.
{{domxref("Document.onvisibilitychange")}}
Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("visibilitychange")}} event is raised.
{{domxref("Document.onwheel")}} {{non-standard_inline}}
Represents the event handling code for the {{event("wheel")}} event.

The Document interface is extended with the {{domxref("GlobalEventHandlers")}} interface:

{{Page("/en-US/docs/Web/API/GlobalEventHandlers", "Properties")}}

Deprecated properties

{{domxref("Document.alinkColor")}} {{Deprecated_inline}}
Returns or sets the color of active links in the document body.
{{domxref("Document.all")}} {{Deprecated_inline}} {{non-standard_inline}}
Provides access to all elements in the document. This is a legacy, non-standard property and should not be used.
{{domxref("Document.applets")}} {{Deprecated_inline}} {{readonlyinline}}
Returns an ordered list of the applets within a document.
{{domxref("Document.async")}} {{Deprecated_inline}}
Used with {{domxref("Document.load")}} to indicate an asynchronous request.
{{domxref("Document.bgColor")}} {{Deprecated_inline}}
Gets/sets the background color of the current document.
{{domxref("Document.charset")}} {{readonlyinline}} {{Deprecated_inline}}
Alias of {{domxref("Document.characterSet")}}. Use this property instead.
{{domxref("Document.domConfig")}} {{Deprecated_inline}}
Should return a {{domxref("DOMConfiguration")}} object.
{{domxref("document.fgColor")}} {{Deprecated_inline}}
Gets/sets the foreground color, or text color, of the current document.
{{domxref("Document.fullscreen")}} {{obsolete_inline}}
true when the document is in {{domxref("Using_full-screen_mode","full-screen mode")}}.
{{domxref("Document.height")}} {{non-standard_inline}} {{obsolete_inline}}
Gets/sets the height of the current document.
{{domxref("Document.inputEncoding")}} {{readonlyinline}} {{Deprecated_inline}}
Alias of {{domxref("Document.characterSet")}}. Use this property instead.
{{domxref("Document.linkColor")}} {{Deprecated_inline}}
Gets/sets the color of hyperlinks in the document.
{{domxref("Document.vlinkColor")}} {{Deprecated_inline}}
Gets/sets the color of visited hyperlinks.
{{domxref("Document.width")}} {{non-standard_inline}} {{obsolete_inline}}
Returns the width of the current document.
{{domxref("Document.xmlEncoding")}} {{Deprecated_inline}}
Returns the encoding as determined by the XML declaration.
{{domxref("Document.xmlStandalone")}} {{obsolete_inline("10.0")}}
Returns true if the XML declaration specifies the document to be standalone (e.g., An external part of the DTD affects the document's content), else false.
{{domxref("Document.xmlVersion")}} {{obsolete_inline("10.0")}}
Returns the version number as specified in the XML declaration or "1.0" if the declaration is absent.

Methods

This interface also inherits from the {{domxref("Node")}} and {{domxref("EventTarget")}} interfaces.

{{domxref("Document.adoptNode()")}}
Adopt node from an external document.
{{domxref("Document.captureEvents()")}} {{Deprecated_inline}}
See {{domxref("Window.captureEvents")}}.
{{domxref("Document.caretRangeFromPoint()")}}{{non-standard_inline}}
Gets a {{Domxref("Range")}} object for the document fragment under the specified coordinates.
{{domxref("Document.createAttribute()")}}
Creates a new {{domxref("Attr")}} object and returns it.
{{domxref("Document.createAttributeNS()")}}
Creates a new attribute node in a given namespace and returns it.
{{domxref("Document.createCDATASection()")}}
Creates a new CDATA node and returns it.
{{domxref("Document.createComment()")}}
Creates a new comment node and returns it.
{{domxref("Document.createDocumentFragment()")}}
Creates a new document fragment.
{{domxref("Document.createElement()")}}
Creates a new element with the given tag name.
{{domxref("Document.createElementNS()")}}
Creates a new element with the given tag name and namespace URI.
{{domxref("Document.createEntityReference()")}} {{obsolete_inline}}
Creates a new entity reference object and returns it.
{{domxref("Document.createEvent()")}}
Creates an event object.
{{domxref("Document.createNodeIterator()")}}
Creates a {{domxref("NodeIterator")}} object.
{{domxref("Document.createProcessingInstruction()")}}
Creates a new {{domxref("ProcessingInstruction")}} object.
{{domxref("Document.createRange()")}}
Creates a {{domxref("Range")}} object.
{{domxref("Document.createTextNode()")}}
Creates a text node.
{{domxref("Document.createTouch()")}} {{Deprecated_inline}}
Creates a {{domxref("Touch")}} object.
{{domxref("Document.createTouchList()")}}
Creates a {{domxref("TouchList")}} object.
{{domxref("Document.createTreeWalker()")}}
Creates a {{domxref("TreeWalker")}} object.
{{domxref("Document.enableStyleSheetsForSet()")}}
Enables the style sheets for the specified style sheet set.
{{domxref("Document.exitPointerLock()")}} {{experimental_inline}}
Release the pointer lock.
{{domxref("Document.getAnimations()")}} {{experimental_inline}}
Returns an array of all {{domxref("Animation")}} objects currently in effect, whose target elements are descendants of the document.
{{domxref("Document.getElementsByClassName()")}}
Returns a list of elements with the given class name.
{{domxref("Document.getElementsByTagName()")}}
Returns a list of elements with the given tag name.
{{domxref("Document.getElementsByTagNameNS()")}}
Returns a list of elements with the given tag name and namespace.
{{domxref("Document.importNode()")}}
Returns a clone of a node from an external document.
{{domxref("Document.normalizeDocument()")}} {{obsolete_inline}}
Replaces entities, normalizes text nodes, etc.
{{domxref("Document.releaseCapture()")}} {{non-standard_inline}} {{gecko_minversion_inline("2.0")}}
Releases the current mouse capture if it's on an element in this document.
{{domxref("Document.releaseEvents()")}} {{non-standard_inline}} {{Deprecated_inline}}
See {{domxref("Window.releaseEvents()")}}.
{{domxref("Document.routeEvent()")}} {{non-standard_inline}} {{obsolete_inline(24)}}
See {{domxref("Window.routeEvent()")}}.
{{domxref("Document.mozSetImageElement()")}} {{non-standard_inline}} {{gecko_minversion_inline("2.0")}}
Allows you to change the element being used as the background image for a specified element ID.

The Document interface is extended with the {{domxref("ParentNode")}} interface:

{{domxref("document.getElementById","document.getElementById(String id)")}}
Returns an object reference to the identified element.
{{domxref("document.querySelector","document.querySelector(String selector)")}} {{gecko_minversion_inline("1.9.1")}}
Returns the first Element node within the document, in document order, that matches the specified selectors.
{{domxref("document.querySelectorAll","document.querySelectorAll(String selector)")}} {{gecko_minversion_inline("1.9.1")}}
Returns a list of all the Element nodes within the document that match the specified selectors.

The Document interface is extended with the {{domxref("XPathEvaluator")}} interface:

{{domxref("document.createExpression","document.createExpression(String expression, XPathNSResolver resolver)")}}
Compiles an XPathExpression which can then be used for (repeated) evaluations.
{{domxref("document.createNSResolver","document.createNSResolver(Node resolver)")}}
Creates an {{domxref("XPathNSResolver")}} object.
{{domxref("document.evaluate","document.evaluate(String expression, Node contextNode, XPathNSResolver resolver, Number type, Object result)")}}
Evaluates an XPath expression.

Extension for HTML documents

The Document interface for HTML documents inherit from the {{domxref("HTMLDocument")}} interface or, since HTML5,  is extended for such documents:

{{domxref("document.clear()")}} {{non-standard_inline}} {{Deprecated_inline}}
In majority of modern browsers, including recent versions of Firefox and Internet Explorer, this method does nothing.
{{domxref("document.close()")}}
Closes a document stream for writing.
{{domxref("document.execCommand","document.execCommand(String command[, Boolean showUI[, String value]])")}}
On an editable document, executes a formating command.
{{domxref("document.getElementsByName","document.getElementsByName(String name)")}}
Returns a list of elements with the given name.
{{domxref("document.hasFocus()")}}
Returns true if the focus is currently located anywhere inside the specified document.
{{domxref("document.open()")}}
Opens a document stream for writing.
{{domxref("document.queryCommandEnabled","document.queryCommandEnabled(String command)")}}
Returns true if the formating command can be executed on the current range.
{{domxref("document.queryCommandIndeterm","document.queryCommandIndeterm(String command)")}}
Returns true if the formating command is in an indeterminate state on the current range.
{{domxref("document.queryCommandState","document.queryCommandState(String command)")}}
Returns true if the formating command has been executed on the current range.
{{domxref("document.queryCommandSupported","document.queryCommandSupported(String command)")}}
Returns true if the formating command is supported on the current range.
{{domxref("document.queryCommandValue","document.queryCommandValue(String command)")}}
Returns the current value of the current range for a formating command.
{{domxref("document.write","document.write(String text)")}}
Writes text in a document.
{{domxref("document.writeln","document.writeln(String text)")}}
Writes a line of text in a document.

Methods included from DocumentOrShadowRoot

The Document 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.

{{domxref("DocumentOrShadowRoot.getSelection()")}}
Returns a {{domxref('Selection')}} object representing the range of text selected by the user, or the current position of the caret.
{{domxref("DocumentOrShadowRoot.elementFromPoint()")}}
Returns the topmost element at the specified coordinates.
{{domxref("DocumentOrShadowRoot.elementsFromPoint()")}}
Returns an array of all elements at the specified coordinates.
{{domxref("DocumentOrShadowRoot.caretPositionFromPoint()")}}
Returns a {{domxref('CaretPosition')}} object containing the DOM node containing the caret, and caret's character offset within that node.

Specifications

Specification Status Comment
{{SpecName('Page Visibility API', '#onvisiblitychange-event-handler', 'onvisibilitychange')}} {{Spec2('Page Visibility API')}} Adds onvisibilitychange.
{{SpecName('Selection API', '#onvisiblitychange-event-handler', 'Extend Document and GlobalEventHandlers')}} {{Spec2('Selection API')}} Adds onselectstart and onselectionchange.
{{SpecName('DOM1','#i-Document','Document')}} {{Spec2('DOM1')}} Initial definition for the interface
{{SpecName('DOM2 Core','#i-Document','Document')}} {{Spec2('DOM2 Core')}} Supersede DOM 1
{{SpecName('DOM3 Core','#i-Document','Document')}} {{Spec2('DOM3 Core')}} Supersede DOM 2
{{SpecName('DOM WHATWG','#interface-document','Document')}} {{Spec2('DOM WHATWG')}} Intend to supersede DOM 3
{{SpecName('HTML WHATWG','dom.html#the-document-object','Document')}} {{Spec2('HTML WHATWG')}} Turn the {{domxref("HTMLDocument")}} interface into a Document extension.
{{SpecName('DOM3 XPath','xpath.html#XPathEvaluator','XPathEvaluator')}} {{Spec2('DOM3 XPath')}} Define the {{domxref("XPathEvaluator")}} interface which extend document.
{{SpecName('Page Visibility API', '#sec-document-interface', 'Document')}} {{Spec2('Page Visibility API')}} Extend the Document interface with the visibilityState and hidden attributes
{{SpecName('HTML Editing','#dom-document-getselection','Document')}} {{Spec2('HTML Editing')}} Extend the Document interface
{{SpecName('CSSOM View','#extensions-to-the-document-interface','Document')}} {{Spec2('CSSOM View')}} Extend the Document interface
{{SpecName('CSSOM','#extensions-to-the-document-interface','Document')}} {{Spec2('CSSOM')}} Extend the Document interface
{{SpecName('Pointer Lock','#extensions-to-the-document-interface','Document')}} {{Spec2('Pointer Lock')}} Extend the Document interface

Browser compatibility notes

Firefox notes

Mozilla defines a set of non-standard properties made only for XUL content:

{{domxref("document.currentScript")}} {{non-standard_inline}} {{gecko_minversion_inline("2.0")}}
Returns the {{HTMLElement("script")}} element that is currently executing.
{{domxref("document.documentURIObject")}} {{gecko_minversion_inline("1.9")}}
(Mozilla add-ons only!) Returns the {{Interface("nsIURI")}} object representing the URI of the document. This property only has special meaning in privileged JavaScript code (with UniversalXPConnect privileges).
{{domxref("document.popupNode")}}
Returns the node upon which a popup was invoked.
{{domxref("document.tooltipNode")}}
Returns the node which is the target of the current tooltip.

Mozilla also define some non-standard methods:

{{domxref("document.execCommandShowHelp")}} {{obsolete_inline("14.0")}}
This method never did anything and always threw an exception, so it was removed in Gecko 14.0 {{geckoRelease("14.0")}}.
{{domxref("document.getBoxObjectFor")}} {{obsolete_inline}}
Use the {{domxref("Element.getBoundingClientRect()")}} method instead.
{{domxref("document.loadOverlay")}} {{Fx_minversion_inline("1.5")}}
Loads a XUL overlay dynamically. This only works in XUL documents.
{{domxref("document.queryCommandText")}} {{obsolete_inline("14.0")}}
This method never did anything but throw an exception, and was removed in Gecko 14.0 {{geckoRelease("14.0")}}.

Internet Explorer notes

Microsoft defines some non-standard properties:

{{domxref("document.fileSize")}}* {{non-standard_inline}} {{obsolete_inline}}
Returns size in bytes of the document. Starting with Internet Explorer 11, that property is no longer supported. See MSDN.
Internet Explorer does not support all methods from the Node interface in the Document interface:
{{domxref("document.contains")}}
As a work-around, document.body.contains() can be used.

Browser compatibility

{{Compat("api.Document")}}

文档标签和贡献者

标签: 
最后编辑者: zxsunrise,