这篇翻译不完整。请帮忙从英语翻译这篇文章

Document 接口提供了一些在浏览器服务中作为页面内容入口点而加载的一些页面,也就是 DOM 树。 DOM 树包括诸如 <body><table> 之类的元素,及其他元素。其也为文档(document)提供了全局性的函数,例如获取页面的 URL、在文档中创建新的 element 的函数。

 

Document接口描述了任何类型的文档的公共属性和方法。根据文档的类型 (例如 HTMLXML、SVG,  ... ), 可以使用更大的API: HTML 文档, 以text / html内容类型提供, 也实现了HTMLDocument接口,而SVG 文档实现了 SVGDocument 接口。

属性

 Document 接口也继承自  Node 及 EventTarget 接口。

Document.all
对所有元素进行访问。 这是一个遗留的非标准接口,不应该使用。
Document.async 
与  document.load 一起使用,表示一个异步请求。
Document.characterSet 只读
返回文档使用的字符集。
Document.charset 只读
Document.characterSet的别名。使用本属性代替别名。
Document.compatMode 只读
表示是在 Quirks 模式还是在 Strict 模式下渲染文档的。
Document.contentType 只读  
从当前文档的 MIME 首部返回 Content-Type。
Document.doctype 只读
返回当前文档的 Document Type Definition (DTD)。
Document.documentElement 只读
返回 document 的直属后代元素。对于 HTML 文档来说,返回的一般是 HTML 元素。
Document.documentURI 只读
返回文档的 URL。
Document.domConfig
会返回一个 DOMConfiguration 对象。
Document.hidden 只读
Document.implementation 只读
返回与当前文档相关联的 DOM implementation。
Document.inputEncoding 只读
Document.characterSet 的别名。使用本属性替代 Document.characterSet
Document.lastStyleSheetSet 只读
返回样式表集最后使用的名称。其初始值为 null,直到设置 selectedStyleSheetSet 的值改变这些样式表。
Document.mozSyntheticDocument
当 document 是人造(synthetic)时,如一个独立的图像,视频,音频文件等,值为 true
Document.mozFullScreen
当 document 处于 full-screen mode 时,其值为 true。
Document.mozFullScreenElement 只读
当前 document 处于全屏模式下的元素。
Document.mozFullScreenEnabled 只读
假如在当前 document 调用 element.mozRequestFullscreen() 成功,其值为 true。
Document.pointerLockElement 只读
当指针被锁定时,返回元素集合,作为鼠标事件的目标。 如果锁被挂起,或指针解锁,或者目标是在另一个 document,则为 null。
Document.preferredStyleSheetSet 只读
返回网页设计人员所指定的首选样式表集。
Document.scrollingElement 只读
返回滚动 document 的 Element 的一个引用。 Element that scrolls the document.
Document.selectedStyleSheetSet
返回当前正在使用的样式表集。
Document.styleSheets 只读
返回当前 document 的样式表对象的列表。
Document.styleSheetSets 只读
返回当前 document 可用的样式表集的列表。
Document.timeline 只读
Document.undoManager 只读
Document.URL 只读
返回...
Document.visibilityState 只读
Document.xmlEncoding
返回 XML 声明所确定的编码。
Document.xmlStandalone 已废弃 Gecko 10.0
返回 true,如果 XML 声明指定该 document 是独立的(例如,document 的内容会受到一个 DTD 外部影响 ),否则为 false
Document.xmlVersion 已废弃 Gecko 10.0
返回指定 XML 声明的版本号,如果不存在版本号,则返回"1.0"

Document 接口是 ParentNode 接口的扩展:

ParentNode.children 只读
Returns a live HTMLCollection containing all objects of type Element that are children of this ParentNode.
ParentNode.firstElementChild 只读
Returns the Element that is the first child of this ParentNode, or null if there is none.
ParentNode.lastElementChild 只读
Returns the Element that is the last child of this ParentNode, or null if there is none.
ParentNode.childElementCount 只读
Returns an unsigned long giving the amount of children that the object has.

扩展的HTML文档

HTML文档的 Document 接口继承自 HTMLDocument 接口或者说从 HTML5 之后扩展了下面这些文档:

Document.activeElement 只读
返回当前正在操作的元素
Document.alinkColor
返回或者设置,document中的活动链接的颜色
Document.anchors
返回当前 document 的所有的锚点的列表
Document.applets
返回的 document 内的 applets 的有序列表
Document.bgColor
获取/设置当前文档的背景颜色.
Document.body
返回当前文档的 <body> 元素.
Document.cookie
返回文档的由分号分隔的cookie列表,或者设置一个单独的cookie值.
Document.defaultView 只读
返回对window对象的引用
Document.designMode
获取/设置整个文档的编辑功能
Document.dir 只读
Gets/sets directionality (rtl/ltr) of the document.获取/设置文档的文字朝向(rtl从右到左/ltr从左到右)
Document.domain 只读
获取/设置当前文档的域名.
Document.embeds 只读
返回当前文档中的<embed>元素列表
Document.fgColor
获取/设置当前文档的前景色或字体颜色
Document.forms 只读
返回当前文档中的<form>元素列表
Document.head 只读
返回当前文档的<head>元素
Document.height
获取/设置当前文档的高度
Document.images 只读
返回当前文档的图像列表
Document.lastModified 只读
返回文档最后编辑时间
Document.linkColor
获取/设置文档的超链接的颜色
Document.links 只读
返回文档中所有超链接的列表
Document.location 只读
返回当前文档的URI
Document.plugins 只读
返回可用插件列表
Document.readyState 只读
返回文档的加载状态
Document.referrer 只读
Returns the URI of the page that linked to this page.
Document.scripts 只读
返回文档中所有的<script>元素
Document.title
设置或获取当前文档的标题
Document.URL 只读
返回文档的地址字符串
Document.vlinkColor
获取/设置访问过的超链接的颜色
Document.width
返回当前文档的宽度

事件句柄

Document.onafterscriptexecute
表示afterscriptexecute事件的事件处理代码
Document.onbeforescriptexecute
表示beforescriptexecute事件的事件处理代码
Document.oncopy
表示copy 事件的事件处理代码.
Document.oncut
表示cut 事件的事件处理代码.
Document.onfullscreenchange
Is an EventHandler representing the code to be called when the fullscreenchange event is raised.
Document.onfullscreenerror
Is an EventHandler representing the code to be called when the fullscreenerror event is raised.
Document.onpaste
表示 paste 事件的事件处理代码.
Document.onpointerlockchange
表示 pointerlockchange 事件的事件处理代码.
Document.onpointerlockerror
表示 pointerlockerror 事件的事件处理代码.
Document.onreadystatechange
表示readystatechange 事件的事件处理代码.
Document.onselectionchange
Is an EventHandler representing the code to be called when the selectionchange event is raised.
Document.onvisibilitychange
Is an EventHandler representing the code to be called when the visibilitychange event is raised.
Document.onwheel
Represents the event handling code for the wheel event.

The Document interface is extended with the GlobalEventHandlers interface:

GlobalEventHandlers.onabort
Is an EventHandler representing the code to be called when the abort event is raised.
GlobalEventHandlers.onanimationcancel
An EventHandler called when an animationcancel event is sent, indicating that a running CSS animation has been canceled.
GlobalEventHandlers.onanimationend
An EventHandler called when an animationend event is sent, indicating that a CSS animation has stopped playing.
GlobalEventHandlers.onanimationiteration
An EventHandler called when an animationiteration event has been sent, indicating that a CSS animation has begun playing a new iteration of the animation sequence.
GlobalEventHandlers.onanimationstart
An EventHandler called when an animationstart event is sent, indicating that a CSS animation has started playing.
GlobalEventHandlers.onauxclick
An EventHandler called when an auxclick event is sent, indicating that a non-primary button has been pressed on an input device (e.g. a middle mouse button).
GlobalEventHandlers.onblur
Is an EventHandler representing the code to be called when the blur event is raised.
GlobalEventHandlers.onerror
Is an OnErrorEventHandler representing the code to be called when the error event is raised.
GlobalEventHandlers.onfocus
Is an EventHandler representing the code to be called when the focus event is raised.
GlobalEventHandlers.oncancel
Is an EventHandler representing the code to be called when the cancel event is raised.
GlobalEventHandlers.oncanplay
Is an EventHandler representing the code to be called when the canplay event is raised.
GlobalEventHandlers.oncanplaythrough
Is an EventHandler representing the code to be called when the canplaythrough event is raised.
GlobalEventHandlers.onchange
Is an EventHandler representing the code to be called when the change event is raised.
GlobalEventHandlers.onclick
Is an EventHandler representing the code to be called when the click event is raised.
GlobalEventHandlers.onclose
Is an EventHandler representing the code to be called when the close event is raised.
GlobalEventHandlers.oncontextmenu
Is an EventHandler representing the code to be called when the contextmenu event is raised.
GlobalEventHandlers.oncuechange
Is an EventHandler representing the code to be called when the cuechange event is raised.
GlobalEventHandlers.ondblclick
Is an EventHandler representing the code to be called when the dblclick event is raised.
GlobalEventHandlers.ondrag
Is an EventHandler representing the code to be called when the drag event is raised.
GlobalEventHandlers.ondragend
Is an EventHandler representing the code to be called when the dragend event is raised.
GlobalEventHandlers.ondragenter
Is an EventHandler representing the code to be called when the dragenter event is raised.
GlobalEventHandlers.ondragexit
Is an EventHandler representing the code to be called when the dragexit event is raised.
GlobalEventHandlers.ondragleave
Is an EventHandler representing the code to be called when the dragleave event is raised.
GlobalEventHandlers.ondragover
Is an EventHandler representing the code to be called when the dragover event is raised.
GlobalEventHandlers.ondragstart
Is an EventHandler representing the code to be called when the dragstart event is raised.
GlobalEventHandlers.ondrop
Is an EventHandler representing the code to be called when the drop event is raised.
GlobalEventHandlers.ondurationchange
Is an EventHandler representing the code to be called when the durationchange event is raised.
GlobalEventHandlers.onemptied
Is an EventHandler representing the code to be called when the emptied event is raised.
GlobalEventHandlers.onended
Is an EventHandler representing the code to be called when the ended event is raised.
GlobalEventHandlers.ongotpointercapture

Is an EventHandler representing the code to be called when the gotpointercapture event type is raised.

GlobalEventHandlers.oninput
Is an EventHandler representing the code to be called when the input event is raised.
GlobalEventHandlers.oninvalid
Is an EventHandler representing the code to be called when the invalid event is raised.
GlobalEventHandlers.onkeydown
Is an EventHandler representing the code to be called when the keydown event is raised.
GlobalEventHandlers.onkeypress
Is an EventHandler representing the code to be called when the keypress event is raised.
GlobalEventHandlers.onkeyup
Is an EventHandler representing the code to be called when the keyup event is raised.
GlobalEventHandlers.onload
Is an EventHandler representing the code to be called when the load event is raised.
GlobalEventHandlers.onloadeddata
Is an EventHandler representing the code to be called when the loadeddata event is raised.
GlobalEventHandlers.onloadedmetadata
Is an EventHandler representing the code to be called when the loadedmetadata event is raised.
GlobalEventHandlers.onloadend
Is an EventHandler representing the code to be called when the loadend event is raised (when progress has stopped on the loading of a resource.)
GlobalEventHandlers.onloadstart
Is an EventHandler representing the code to be called when the loadstart event is raised (when progress has begun on the loading of a resource.)
GlobalEventHandlers.onlostpointercapture

Is an EventHandler representing the code to be called when the lostpointercapture event type is raised.

GlobalEventHandlers.onmousedown
Is an EventHandler representing the code to be called when the mousedown event is raised.
GlobalEventHandlers.onmouseenter
Is an EventHandler representing the code to be called when the mouseenter event is raised.
GlobalEventHandlers.onmouseleave
Is an EventHandler representing the code to be called when the mouseleave event is raised.
GlobalEventHandlers.onmousemove
Is an EventHandler representing the code to be called when the mousemove event is raised.
GlobalEventHandlers.onmouseout
Is an EventHandler representing the code to be called when the mouseout event is raised.
GlobalEventHandlers.onmouseover
Is an EventHandler representing the code to be called when the mouseover event is raised.
GlobalEventHandlers.onmouseup
Is an EventHandler representing the code to be called when the mouseup event is raised.
GlobalEventHandlers.onmousewheel
Is an EventHandler representing the code to be called when the mousewheel event is raised.
GlobalEventHandlers.onwheel
Is an EventHandler representing the code to be called when the wheel event is raised.
GlobalEventHandlers.onpause
Is an EventHandler representing the code to be called when the pause event is raised.
GlobalEventHandlers.onplay
Is an EventHandler representing the code to be called when the play event is raised.
GlobalEventHandlers.onplaying
Is an EventHandler representing the code to be called when the playing event is raised.
GlobalEventHandlers.onpointerdown
Is an EventHandler representing the code to be called when the pointerdown event is raised.
GlobalEventHandlers.onpointermove
Is an EventHandler representing the code to be called when the pointermove event is raised.
GlobalEventHandlers.onpointerup
Is an EventHandler representing the code to be called when the pointerup event is raised.
GlobalEventHandlers.onpointercancel
Is an EventHandler representing the code to be called when the pointercancel event is raised.
GlobalEventHandlers.onpointerover
Is an EventHandler representing the code to be called when the pointerover event is raised.
GlobalEventHandlers.onpointerout
Is an EventHandler representing the code to be called when the pointerout event is raised.
GlobalEventHandlers.onpointerenter
Is an EventHandler representing the code to be called when the pointerevent event is raised.
GlobalEventHandlers.onpointerleave
Is an EventHandler representing the code to be called when the pointerleave event is raised.
GlobalEventHandlers.onpointerlockchange
Is an EventHandler representing the code to be called when the pointerlockchange event is raised.
GlobalEventHandlers.onpointerlockerror
Is an EventHandler representing the code to be called when the pointerlockerror event is raised.
GlobalEventHandlers.onprogress
Is an EventHandler representing the code to be called when the progress event is raised.
GlobalEventHandlers.onratechange
Is an EventHandler representing the code to be called when the ratechange event is raised.
GlobalEventHandlers.onreset
Is an EventHandler representing the code to be called when the reset event is raised.
GlobalEventHandlers.onscroll
Is an EventHandler representing the code to be called when the scroll event is raised.
GlobalEventHandlers.onseeked
Is an EventHandler representing the code to be called when the seeked event is raised.
GlobalEventHandlers.onseeking
Is an EventHandler representing the code to be called when the seeking event is raised.
GlobalEventHandlers.onselect
Is an EventHandler representing the code to be called when the select event is raised.
GlobalEventHandlers.onselectstart
Is an EventHandler representing the code to be called when the selectionchange event is raised, i.e. when the user starts to make a new text selection on a web page.
GlobalEventHandlers.onselectionchange
Is an EventHandler representing the code to be called when the selectionchange event is raised, i.e. when the text selected on a web page changes.
GlobalEventHandlers.onshow
Is an EventHandler representing the code to be called when the show event is raised.
GlobalEventHandlers.onsort
Is an EventHandler representing the code to be called when the sort event is raised.
GlobalEventHandlers.onstalled
Is an EventHandler representing the code to be called when the stalled event is raised.
GlobalEventHandlers.onsubmit
Is an EventHandler representing the code to be called when the submit event is raised.
GlobalEventHandlers.onsuspend
Is an EventHandler representing the code to be called when the suspend event is raised.
GlobalEventHandlers.ontimeupdate
Is an EventHandler representing the code to be called when the timeupdate event is raised.
GlobalEventHandlers.onvolumechange
Is an EventHandler representing the code to be called when the volumechange event is raised.
GlobalEventHandlers.ontouchcancel
Is an EventHandler representing the code to be called when the touchcancel event is raised.
GlobalEventHandlers.ontouchend
Is an EventHandler representing the code to be called when the touchend event is raised.
GlobalEventHandlers.ontouchmove
Is an EventHandler representing the code to be called when the touchmove event is raised.
GlobalEventHandlers.ontouchstart
Is an EventHandler representing the code to be called when the touchstart event is raised.
GlobalEventHandlers.ontransitioncancel
An EventHandler called when a transitioncancel event is sent, indicating that a CSS transition has been cancelled.
GlobalEventHandlers.ontransitionend
An EventHandler called when a transitionend event is sent, indicating that a CSS transition has finished playing.
GlobalEventHandlers.onwaiting
Is an EventHandler representing the code to be called when the waiting event is raised.

方法

文档接口也继承自NodeEventTarget接口

Document.adoptNode()
从外部文档接收节点.
Document.captureEvents()
查看window.captureEvents.
Document.caretPositionFromPoint()
在或接近给定的坐标处获取CaretPosition
Document.caretRangeFromPoint()
Gets a Range object for the document fragment under the specified coordinates.
Document.createAttribute(String name)
创建一个新的 Attr 对象并返回.
Document.createAttributeNS()
在给定的命名空间内创建一个新的属性节点并返回它
Document.createCDATASection()
创建一个新的CDATA节点并返回它
Document.createComment()
创建一个新的注释节点并返回它
Document.createDocumentFragment()
创建一个新的文档片段
Document.createElement()
用给定的标签名创建一个新的元素
Document.createElementNS()
用给定的标签名和命名空间URI创建一个新的元素
Document.createEntityReference()
创建一个新的实体引用对象并返回
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()
Enables the style sheets for the specified style sheet set.
Document.exitPointerLock()
Release the pointer lock.
Document.getAnimations()
Returns an array of all Animation objects currently in effect, whose target elements are descendants of the document.
Document.getElementsByClassName()
返回有给定样式名的元素列表
Document.getElementsByTagName()
返回有给定标签名的元素列表
Document.getElementsByTagNameNS()
返回有给定标签名和命名空间的元素列表
Document.importNode()
返回一个从外部文档中一个节点的拷贝
Document.normalizeDocument()
Replaces entities, normalizes text nodes, etc.
Document.registerElement()
注册一个web component
Document.releaseCapture()
Releases the current mouse capture if it's on an element in this document.
Document.releaseEvents()
查看 Window.releaseEvents().
Document.routeEvent() 已废弃 Gecko 24
查看 Window.routeEvent().
Document.mozSetImageElement()
Allows you to change the element being used as the background image for a specified element ID.

The Document interface is extended with the ParentNode interface:

document.getElementById(String id)
返回一个对识别元素的对象引用
document.querySelector(String selector)
返回文档中第一个匹配指定选择器的元素
document.querySelectorAll(String selector)
返回一个匹配指定选择器的元素节点列表

The Document interface is extended with the XPathEvaluator interface:

document.createExpression(String expression, XPathNSResolver resolver)
Compiles an XPathExpression which can then be used for (repeated) evaluations.
document.createNSResolver(Node resolver)
创建一个 XPathNSResolver 对象.
document.evaluate(String expression, Node contextNode, XPathNSResolver resolver, Number type, Object result)
Evaluates an XPath expression.

扩展的HTML文档

HTML文档的 Document 接口继承自 HTMLDocument 接口或者说从 HTML5 之后扩展了下面这些文档:

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

Specifications

Specification Status Comment
Document Object Model (DOM) Level 1 Specification
Document
Obsolete Initial definition for the interface
Document Object Model (DOM) Level 2 Core Specification
Document
Obsolete Supersede DOM 1
Document Object Model (DOM) Level 3 Core Specification
Document
Obsolete Supersede DOM 2
DOM
Document
Living Standard Intend to supersede DOM 3
HTML Living Standard
Document
Living Standard Turn the HTMLDocument interface into a Documentextension.
Document Object Model (DOM) Level 3 XPath Specification
XPathEvaluator
Recommendation Define the XPathEvaluator interface which extendDocument.
HTML Editing APIs
Document
Editor's Draft Extend the Documentinterface
CSS Object Model (CSSOM) View Module
Document
Working Draft Extend the Documentinterface
CSS Object Model (CSSOM)
Document
Working Draft Extend the Documentinterface
Pointer Lock
Document
Candidate Recommendation Extend the Documentinterface

Browser compatibility

Firefox notes

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

document.currentScript
Returns the <script> element that is currently executing.
document.documentURIObject
(Mozilla add-ons only!) Returns the nsIURI object representing the URI of the document. This property only has special meaning in privileged JavaScript code (with UniversalXPConnect privileges).
document.popupNode
Returns the node upon which a popup was invoked.
document.tooltipNode
Returns the node which is the target of the current tooltip.

Mozilla also define some non-standard methods:

Document.execCommandShowHelp 已废弃 Gecko 14.0
This method never did anything and always threw an exception, so it was removed in Gecko 14.0 (Firefox 14.0 / Thunderbird 14.0 / SeaMonkey 2.11).
Document.getBoxObjectFor
Use the Element.getBoundingClientRect() method instead.
Document.loadOverlay
Loads a XUL overlay dynamically. This only works in XUL documents.
document.queryCommandText 已废弃 Gecko 14.0
This method never did anything but throw an exception, and was removed in Gecko 14.0 (Firefox 14.0 / Thunderbird 14.0 / SeaMonkey 2.11).

Internet Explorer notes

Microsoft defines some non-standard properties:

document.fileSize*
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 Documentinterface:
document.contains
As a work-around, document.body.contains() can be used.

文档标签和贡献者

标签: 
 最后编辑者: _sollrei,