mozilla
您的搜索结果

    Document

     

    摘要

    每个在浏览器中装载的web页面都有自己的  document 对象。Document 接口可以作为web页面内容(DOM tree, 包含如 <body><table> 等元素)的入口,并提供了文档的全局性的操作函数(例如在文档中获取一个页面的URL 以及创建一个新的元素)。

     document 对象可通过多个API获取到:

    根据文档的类型(如 HTML 或 XML ),对文档对象操作的API也会不同。关于API的理论可用性经常在 相关 W3C DOM 规范的接口实现中有所描述:

    • 所有的文档对象都实现了 DOM 核心 Document 和 Node接口,,也就是说核心属性和方法在所有类型的文档中多是可获得的。
    • 除了一般的DOM核心文档接口:
      • HTML Document 同样实现了 HTMLDocument 接口, 这是在处理HTML 文档时针对性更强的接口。
      • SVG document 实现了 SVGDocument 接口。

    属性

    注意:  Document 接口也继承自  NodeEventTarget 接口。

    Document.all
    通过id可对所有元素进行访问。 这是一个遗留的非标准接口,可以使用  Document.getElementById() 方法来替代。
    Document.async
    与  document.load 一起使用,表示一个异步请求。
    Document.characterSet
    返回文档使用的字符集。
    Document.compatMode
    Indicates whether the document is rendered in Quirks or Strict mode.
    Document.contentType
    Returns the Content-Type from the MIME Header of the current document.
    Document.doctype
    Returns the Document Type Definition (DTD) of the current document.
    Document.documentElement
    Returns the Element that is a direct child of document. For HTML documents, this is normally the HTML element.
    Document.documentURI
    Returns the document URL.
    Document.domConfig
    Should return a DOMConfiguration object.
    Document.implementation
    Returns the DOM implementation associated with the current document.
    Document.inputEncoding
    Returns the encoding used when the document was parsed.
    Document.lastStyleSheetSet
    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 selectedStyleSheetSet.
    Document.mozSyntheticDocument
    true if this document is synthetic, such as a standalone image, video, audio file, or the like.
    Document.mozFullScreen
    true when the document is in full-screen mode.
    Document.mozFullScreenElement
    The element that's currently in full screen mode for this document.
    Document.mozFullScreenEnabled
    true if calling element.mozRequestFullscreen() would succeed in the curent document.
    Document.pointerLockElement
    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.
    Document.preferredStyleSheetSet
    Returns the preferred style sheet set as specified by the page author.
    Document.selectedStyleSheetSet
    Returns which style sheet set is currently in use.
    Document.styleSheets
    Returns a list of the style sheet objects on the current document.
    Document.styleSheetSets
    Returns a list of the style sheet sets available on the document.
    Document.xmlEncoding
    Returns the encoding as determined by the XML declaration.
    Document.xmlStandalone 已废弃 Gecko 10.0
    Returns true if the XML declaration specifies the document is standalone (e.g., An external part of the DTD affects the document's content), else false.
    Document.xmlVersion 已废弃 Gecko 10.0
    Returns the version number as specified in the XML declaration or "1.0" if the declaration is absent.

    The Document interface is extended with the ParentNode interface:

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

    Extention for HTML document

    The Document interface for HTML documents inherit from the HTMLDocument interface or, since HTML5,  is extended for such documents:

    Document.activeElement
    Returns the currently focused element.
    Document.alinkColor
    Returns or sets the color of active links in the document body.
    Document.anchors
    Returns a list of all of the anchors in the document.
    Document.applets
    Returns an ordered list of the applets within a document.
    Document.bgColor
    Gets/sets the background color of the current document.
    Document.body
    Returns the <body> element of the current document.
    Document.cookie
    Returns a semicolon-separated list of the cookies for that document or sets a single cookie.
    Document.defaultView
    Returns a reference to the window object.
    Document.designMode
    Gets/sets the ability to edit the whole document.
    Document.dir
    Gets/sets directionality (rtl/ltr) of the document.
    Document.domain
    Returns the domain of the current document.
    Document.embeds
    Returns a list of the embedded <embed> elements within the current document.
    Document.fgColor
    Gets/sets the foreground color, or text color, of the current document.
    Document.forms
    Returns a list of the <form> elements within the current document.
    Document.head
    Returns the <head> element of the current document.
    Document.height
    Gets/sets the height of the current document.
    Document.images
    Returns a list of the images in the current document.
    Document.lastModified
    Returns the date on which the document was last modified.
    Document.linkColor
    Gets/sets the color of hyperlinks in the document.
    Document.links
    Returns a list of all the hyperlinks in the document.
    Document.location
    Returns the URI of the current document.
    Document.plugins
    Returns a list of the available plugins.
    Document.readyState
    Returns loading status of the document.
    Document.referrer
    Returns the URI of the page that linked to this page.
    Document.scripts
    Returns all the <script> elements on the document.
    Document.title
    Returns the title of the current document.
    Document.URL
    Returns a string containing the URL of the current document.
    Document.vlinkColor
    Gets/sets the color of visited hyperlinks.
    Document.width
    Returns the width of the current document.

    Event handlers

    Document.onpointerlockchange
    Returns the event handling code for the pointerlockchange event.
    Document.onpointerlockerror
    Returns the event handling code for the pointerlockerror event.
    Document.onreadystatechange
    Returns the event handling code for the readystatechange event.

    Methods

    Note: The Document interface also inherit from the Node and EventTarget interfaces.

    Document.adoptNode(Node node)
    Adopt node from an external document.
    Document.captureEvents(String eventName)
    See window.captureEvents.
    Document.caretPositionFromPoint(Number x, Number y)
    Gets a CaretPosition based on two coordinates.
    Document.createAttribute(String name)
    Creates a new Attr object and returns it.
    Document.createAttributeNS(String namespace, String name)
    Creates a new attribute node in a given namespace and returns it.
    Document.createCDATASection(String data)
    Creates a new CDATA node and returns it.
    Document.createComment(String comment)
    Creates a new comment node and returns it.
    Document.createDocumentFragment()
    Creates a new document fragment.
    Document.createElement(String name)
    Creates a new element with the given tag name.
    Document.createElementNS(String namespace, String name)
    Creates a new element with the given tag name and namespace URI.
    Document.createEntityReference(String name)
    Creates a new entity reference object and returns it.
    Document.createEvent(String interface)
    Creates an event object.
    Document.createNodeIterator(Node root[, Number whatToShow[, NodeFilter filter]])
    Creates a NodeIterator object.
    Document.createProcessingInstruction(String target, String data)
    Creates a new ProcessingInstruction object.
    Document.createRange()
    Creates a Range object.
    Document.createTextNode(String text)
    Creates a text node.
    Document.createTreeWalker(Node root[, Number whatToShow[, NodeFilter filter]])
    Creates a TreeWalker object.
    Document.elementFromPoint(Number x, Number y)
    Returns the element visible at the specified coordinates.
    Document.enableStyleSheetsForSet(String name)
    Enables the style sheets for the specified style sheet set.
    Document.exitPointerLock()
    Release the pointer lock.
    Document.getElementsByClassName(String className)
    Returns a list of elements with the given class name.
    Document.getElementsByTagName(String tagName)
    Returns a list of elements with the given tag name.
    Document.getElementsByTagNameNS(String namespace, String tagName)
    Returns a list of elements with the given tag name and namespace.
    Document.importNode(Node node, Boolean deep)
    Returns a clone of a node from an external document.
    Document.normalizeDocument()
    Replaces entities, normalizes text nodes, etc.
    Document.releaseCapture()
    Releases the current mouse capture if it's on an element in this document.
    Document.releaseEvents
    See window.releaseEvents.
    document.routeEvent 已废弃 Gecko 24
    See 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)
    Returns an object reference to the identified element.
    Document.querySelector(String selector)
    Returns the first Element node within the document, in document order, that matches the specified selectors.
    Document.querySelectorAll(String selector)
    Returns a list of all the Element nodes within the document that match the specified selectors.

    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)
    Creates an XPathNSResolver object.
    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 HTMLDocument interface or, since HTML5,  is extended for such documents:

    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
    Recommendation Initial definition for the interface
    Document Object Model (DOM) Level 2 Core Specification
    Document
    Recommendation Supersede DOM 1
    Document Object Model (DOM) Level 3 Core Specification
    Document
    Recommendation Supersede DOM 2
    DOM
    Document
    Living Standard Intend to supersede DOM 3
    WHATWG 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.

    文档标签和贡献者

    此页面的贡献者有: ReyCG_sub, TigerSoldier, JarodWang, ziyunfei, jsx, less, teoli, Crash
    最后编辑者: teoli,
    隐藏侧边栏