Element - Web APIs 编辑
Element
is the most general base class from which all element objects (i.e. objects that represent elements) in a Document
inherit. It only has methods and properties common to all kinds of elements. More specific classes inherit from Element
. For example, the HTMLElement
interface is the base interface for HTML elements, while the SVGElement
interface is the basis for all SVG elements. Most functionality is specified further down the class hierarchy.
Languages outside the realm of the Web platform, like XUL through the XULElement
interface, also implement Element
.
<div id="interfaceDiagram" style="display: inline-block; position: relative; width: 100%; padding-bottom: 11.666666666666666%; vertical-align: middle; overflow: hidden;"><svg style="display: inline-block; position: absolute; top: 0; left: 0;" viewbox="-50 0 600 70" preserveAspectRatio="xMinYMin meet"><a xlink:href="https://developer.mozilla.org/wiki/en-US/docs/Web/API/EventTarget" target="_top"><rect x="1" y="1" width="110" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text x="56" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">EventTarget</text></a><polyline points="111,25 121,20 121,30 111,25" stroke="#D4DDE4" fill="none"/><line x1="121" y1="25" x2="151" y2="25" stroke="#D4DDE4"/><a xlink:href="https://developer.mozilla.org/wiki/en-US/docs/Web/API/Node" target="_top"><rect x="151" y="1" width="75" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text x="188.5" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">Node</text></a><polyline points="226,25 236,20 236,30 226,25" stroke="#D4DDE4" fill="none"/><line x1="236" y1="25" x2="266" y2="25" stroke="#D4DDE4"/><a xlink:href="https://developer.mozilla.org/wiki/en-US/docs/Web/API/Element" target="_top"><rect x="266" y="1" width="75" height="50" fill="#F4F7F8" stroke="#D4DDE4" stroke-width="2px" /><text x="303.5" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">Element</text></a></svg></div>
a:hover text { fill: #0095DD; pointer-events: all;}
Properties
Inherits properties from its parent interface, Node
, and by extension that interface's parent, EventTarget
. It implements the properties of ParentNode
, ChildNode
, NonDocumentTypeChildNode
, and Animatable
.
Element.attributes
Read only- Returns a
NamedNodeMap
object containing the assigned attributes of the corresponding HTML element. Element.classList
Read only- Returns a
DOMTokenList
containing the list of class attributes. Element.className
- Is a
DOMString
representing the class of the element. Element.clientHeight
Read only- Returns a
Number
representing the inner height of the element. Element.clientLeft
Read only- Returns a
Number
representing the width of the left border of the element. Element.clientTop
Read only- Returns a
Number
representing the width of the top border of the element. Element.clientWidth
Read only- Returns a
Number
representing the inner width of the element. Element.computedName
Read only- Returns a
DOMString
containing the label exposed to accessibility. Element.computedRole
Read only- Returns a
DOMString
containing the ARIA role that has been applied to a particular element. Element.id
- Is a
DOMString
representing the id of the element. Element.innerHTML
- Is a
DOMString
representing the markup of the element's content. Element.localName
Read only- A
DOMString
representing the local part of the qualified name of the element. Element.namespaceURI
Read only- The namespace URI of the element, or
null
if it is no namespace.Note: In Firefox 3.5 and earlier, HTML elements are in no namespace. In later versions, HTML elements are in the
http://www.w3.org/1999/xhtml
namespace in both HTML and XML trees. NonDocumentTypeChildNode.nextElementSibling
Read only- Is an
Element
, the element immediately following the given one in the tree, ornull
if there's no sibling node. Element.outerHTML
- Is a
DOMString
representing the markup of the element including its content. When used as a setter, replaces the element with nodes parsed from the given string. Element.part
- Represents the part identifier(s) of the element (i.e. set using the
part
attribute), returned as aDOMTokenList
. Element.prefix
Read only- A
DOMString
representing the namespace prefix of the element, ornull
if no prefix is specified. NonDocumentTypeChildNode.previousElementSibling
Read only- Is a
Element
, the element immediately preceding the given one in the tree, ornull
if there is no sibling element. Element.scrollHeight
Read only- Returns a
Number
representing the scroll view height of an element. Element.scrollLeft
- Is a
Number
representing the left scroll offset of the element. Element.scrollLeftMax
Read only- Returns a
Number
representing the maximum left scroll offset possible for the element. Element.scrollTop
- A
Number
representing number of pixels the top of the document is scrolled vertically. Element.scrollTopMax
Read only- Returns a
Number
representing the maximum top scroll offset possible for the element. Element.scrollWidth
Read only- Returns a
Number
representing the scroll view width of the element. Element.shadowRoot
Read only- Returns the open shadow root that is hosted by the element, or null if no open shadow root is present.
Element.openOrClosedShadowRoot
Read only- Returns the shadow root that is hosted by the element, regardless if its open or closed. Available only to WebExtensions.
Element.slot
- Returns the name of the shadow DOM slot the element is inserted in.
Element.tabStop
- Is a
Boolean
indicating if the element can receive input focus via the tab key. Element.tagName
Read only- Returns a
String
with the name of the tag for the given element. Element.undoManager
Read only- Returns the
UndoManager
associated with the element. Element.undoScope
- Is a
Boolean
indicating if the element is an undo scope host, or not.
Note: DOM Level 3 defined namespaceURI
, localName
and prefix
on the Node
interface. In DOM4 they were moved to Element
.
This change is implemented in Chrome since version 46.0 and Firefox since version 48.0.
Properties included from Slotable
The Element
interface includes the following property, defined on the Slotable
mixin.
Slotable.assignedSlot
Read only- Returns a
HTMLSlotElement
representing the<slot>
the node is inserted in.
Event handlers
Element.onfullscreenchange
- An event handler for the
fullscreenchange
event, which is sent when the element enters or exits full-screen mode. This can be used to watch both for successful expected transitions, but also to watch for unexpected changes, such as when your app is running in the background. Element.onfullscreenerror
- An event handler for the
fullscreenerror
event, which is sent when an error occurs while attempting to change into full-screen mode.
Methods
Inherits methods from its parents Node
, and its own parent, EventTarget
, and implements those of ParentNode
, ChildNode
, NonDocumentTypeChildNode
, and Animatable
.
EventTarget.addEventListener()
- Registers an event handler to a specific event type on the element.
Element.attachShadow()
- Attaches a shadow DOM tree to the specified element and returns a reference to its
ShadowRoot
. Element.animate()
- A shortcut method to create and run an animation on an element. Returns the created Animation object instance.
Element.closest()
- Returns the
Element
which is the closest ancestor of the current element (or the current element itself) which matches the selectors given in parameter. Element.createShadowRoot()
- Creates a shadow DOM on the element, turning it into a shadow host. Returns a
ShadowRoot
. Element.computedStyleMap()
- Returns a
StylePropertyMapReadOnly
interface which provides a read-only representation of a CSS declaration block that is an alternative toCSSStyleDeclaration
. EventTarget.dispatchEvent()
- Dispatches an event to this node in the DOM and returns a
Boolean
that indicates whether no handler canceled the event. Element.getAnimations()
- Returns an array of Animation objects currently active on the element.
Element.getAttribute()
- Retrieves the value of the named attribute from the current node and returns it as an
Object
. Element.getAttributeNames()
- Returns an array of attribute names from the current element.
Element.getAttributeNS()
- Retrieves the value of the attribute with the specified name and namespace, from the current node and returns it as an
Object
. Element.getBoundingClientRect()
- Returns the size of an element and its position relative to the viewport.
Element.getClientRects()
- Returns a collection of rectangles that indicate the bounding rectangles for each line of text in a client.
Element.getElementsByClassName()
- Returns a live
HTMLCollection
that contains all descendants of the current element that possess the list of classes given in the parameter. Element.getElementsByTagName()
- Returns a live
HTMLCollection
containing all descendant elements, of a particular tag name, from the current element. Element.getElementsByTagNameNS()
- Returns a live
HTMLCollection
containing all descendant elements, of a particular tag name and namespace, from the current element. Element.hasAttribute()
- Returns a
Boolean
indicating if the element has the specified attribute or not. Element.hasAttributeNS()
- Returns a
Boolean
indicating if the element has the specified attribute, in the specified namespace, or not. Element.hasAttributes()
- Returns a
Boolean
indicating if the element has one or more HTML attributes present. Element.hasPointerCapture()
- Indicates whether the element on which it is invoked has pointer capture for the pointer identified by the given pointer ID.
Element.insertAdjacentElement()
- Inserts a given element node at a given position relative to the element it is invoked upon.
Element.insertAdjacentHTML()
- Parses the text as HTML or XML and inserts the resulting nodes into the tree in the position given.
Element.insertAdjacentText()
- Inserts a given text node at a given position relative to the element it is invoked upon.
Element.matches()
- Returns a
Boolean
indicating whether or not the element would be selected by the specified selector string. Element.pseudo()
- Returns a
CSSPseudoElement
representing the child pseudo-element matched by the specified pseudo-element selector. Element.querySelector()
- Returns the first
Node
which matches the specified selector string relative to the element. Element.querySelectorAll()
- Returns a
NodeList
of nodes which match the specified selector string relative to the element. Element.releasePointerCapture()
- Releases (stops) pointer capture that was previously set for a specific
pointer event
. ChildNode.remove()
- Removes the element from the children list of its parent.
Element.removeAttribute()
- Removes the named attribute from the current node.
Element.removeAttributeNS()
- Removes the attribute with the specified name and namespace, from the current node.
EventTarget.removeEventListener()
- Removes an event listener from the element.
Element.requestFullscreen()
- Asynchronously asks the browser to make the element full-screen.
Element.requestPointerLock()
- Allows to asynchronously ask for the pointer to be locked on the given element.
Element.scroll()
- Scrolls to a particular set of coordinates inside a given element.
Element.scrollBy()
- Scrolls an element by the given amount.
Element.scrollIntoView()
- Scrolls the page until the element gets into the view.
Element.scrollTo()
- Scrolls to a particular set of coordinates inside a given element.
Element.setAttribute()
- Sets the value of a named attribute of the current node.
Element.setAttributeNS()
- Sets the value of the attribute with the specified name and namespace, from the current node.
Element.setCapture()
- Sets up mouse event capture, redirecting all mouse events to this element.
Element.setPointerCapture()
- Designates a specific element as the capture target of future pointer events.
Element.toggleAttribute()
- Toggles a boolean attribute, removing it if it is present and adding it if it is not present, on the specified element.
Obsolete methods
Element.getAttributeNode()
- Retrieves the node representation of the named attribute from the current node and returns it as an
Attr
. Element.getAttributeNodeNS()
- Retrieves the node representation of the attribute with the specified name and namespace, from the current node and returns it as an
Attr
. Element.removeAttributeNode()
- Removes the node representation of the named attribute from the current node.
Element.setAttributeNode()
- Sets the node representation of the named attribute from the current node.
Element.setAttributeNodeNS()
- Sets the node representation of the attribute with the specified name and namespace, from the current node.
Events
Listen to these events using addEventListener()
or by assigning an event listener to the oneventname
property of this interface.
cancel
- Fires on a
<dialog>
when the user instructs the browser that they wish to dismiss the current open dialog. For example, the browser might fire this event when the user presses the Esc key or clicks a "Close dialog" button which is part of the browser's UI.
Also available via theoncancel
property. error
- Fired when a resource failed to load, or can't be used. For example, if a script has an execution error or an image can't be found or is invalid.
Also available via theonerror
property. scroll
- Fired when the document view or an element has been scrolled.
Also available via theonscroll
property. select
- Fired when some text has been selected.
Also available via theonselect
property. show
- Fired when a
contextmenu
event was fired on/bubbled to an element that has acontextmenu
attribute.
Also available via theonshow
property. wheel
- Fired when the user rotates a wheel button on a pointing device (typically a mouse).
Also available via theonwheel
property.
Clipboard events
copy
- Fired when the user initiates a copy action through the browser's user interface.
Also available via theoncopy
property. cut
- Fired when the user initiates a cut action through the browser's user interface.
Also available via theoncut
property. paste
- Fired when the user initiates a paste action through the browser's user interface.
Also available via theonpaste
property.
Composition events
compositionend
- Fired when a text composition system such as an input method editor completes or cancels the current composition session.
compositionstart
- Fired when a text composition system such as an input method editor starts a new composition session.
compositionupdate
- Fired when a new character is received in the context of a text composition session controlled by a text composition system such as an input method editor.
Focus events
blur
- Fired when an element has lost focus.
Also available via theonblur
property. focus
- Fired when an element has gained focus.
Also available via theonfocus
property focusin
- Fired when an element is about to gain focus.
focusout
- Fired when an element is about to lose focus.
Fullscreen events
fullscreenchange
- Sent to an
Element
when it transitions into or out of full-screen mode.
Also available via theonfullscreenchange
property. fullscreenerror
- Sent to an
Element
if an error occurs while attempting to switch it into or out of full-screen mode.
Also available via theonfullscreenerror
property.
Keyboard events
keydown
- Fired when a key is pressed.
Also available via theonkeydown
property. keypress
- Fired when a key that produces a character value is pressed down.
Also available via theonkeypress
property. keyup
- Fired when a key is released.
Also available via theonkeyup
property.
Mouse events
auxclick
- Fired when a non-primary pointing device button (e.g., any mouse button other than the left button) has been pressed and released on an element.
Also available via theonauxclick
property. click
- Fired when a pointing device button (e.g., a mouse's primary button) is pressed and released on a single element.
Also available via theonclick
property. contextmenu
- Fired when the user attempts to open a context menu.
Also available via theoncontextmenu
property. dblclick
- Fired when a pointing device button (e.g., a mouse's primary button) is clicked twice on a single element.
Also available via theondblclick
property. DOMActivate
- Occurs when an element is activated, for instance, through a mouse click or a keypress.
mousedown
- Fired when a pointing device button is pressed on an element.
Also available via theonmousedown
property. mouseenter
- Fired when a pointing device (usually a mouse) is moved over the element that has the listener attached.
Also available via theonmouseenter
property. mouseleave
- Fired when the pointer of a pointing device (usually a mouse) is moved out of an element that has the listener attached to it.
Also available via theonmouseleave
property. mousemove
- Fired when a pointing device (usually a mouse) is moved while over an element.
Also available via theonmousemove
property. mouseout
- Fired when a pointing device (usually a mouse) is moved off the element to which the listener is attached or off one of its children.
Also available via theonmouseout
property. mouseover
- Fired when a pointing device is moved onto the element to which the listener is attached or onto one of its children.
Also available via theonmouseover
property. mouseup
- Fired when a pointing device button is released on an element.
Also available via theonmouseup
property. webkitmouseforcechanged
- Fired each time the amount of pressure changes on the trackpadtouchscreen.
webkitmouseforcedown
- Fired after the mousedown event as soon as sufficient pressure has been applied to qualify as a "force click".
webkitmouseforcewillbegin
- Fired before the
mousedown
event. webkitmouseforceup
- Fired after the
webkitmouseforcedown
event as soon as the pressure has been reduced sufficiently to end the "force click".
Touch events
touchcancel
- Fired when one or more touch points have been disrupted in an implementation-specific manner (for example, too many touch points are created).
Also available via theontouchcancel
property. touchend
- Fired when one or more touch points are removed from the touch surface.
Also available via theontouchend
property touchmove
- Fired when one or more touch points are moved along the touch surface.
Also available via theontouchmove
property touchstart
- Fired when one or more touch points are placed on the touch surface.
Also available via theontouchstart
property
Specifications
Specification | Status | Comment |
---|---|---|
CSS Pseudo-Elements Level 4 The definition of 'Element' in that specification. | Working Draft | Added the pseudo() method. |
Web Animations | Working Draft | Added the getAnimations() method. |
UndoManager and DOMTransaction The definition of 'Element' in that specification. | Editor's Draft | Added the undoScope and undoManager properties. |
Pointer Events – Level 2 The definition of 'Element' in that specification. | Recommendation | Added the following event handlers: ongotpointercapture and onlostpointercapture .Added the following methods: setPointerCapture() and releasePointerCapture() . |
Pointer Events The definition of 'Element' in that specification. | Obsolete | Added the following event handlers: ongotpointercapture and onlostpointercapture .Added the following methods: setPointerCapture() and releasePointerCapture() . |
Selectors API Level 1 The definition of 'Element' in that specification. | Obsolete | Added the following methods: querySelector() and querySelectorAll() . |
Pointer Lock The definition of 'Element' in that specification. | Candidate Recommendation | Added the requestPointerLock() method. |
Fullscreen API The definition of 'Element' in that specification. | Living Standard | Added the requestFullscreen() method. |
DOM Parsing and Serialization The definition of 'Element' in that specification. | Working Draft | Added the following properties: innerHTML , and outerHTML .Added the following method: insertAdjacentHTML() . |
CSS Object Model (CSSOM) View Module The definition of 'Element' in that specification. | Working Draft | Added the following properties: scrollTop , scrollLeft , scrollWidth , scrollHeight , clientTop , clientLeft , clientWidth , and clientHeight .Added the following methods: getClientRects() , getBoundingClientRect() , scroll() , scrollBy() , scrollTo() and scrollIntoView() . |
Element Traversal Specification The definition of 'Element' in that specification. | Obsolete | Added inheritance of the ElementTraversal interface. |
DOM The definition of 'Element' in that specification. | Living Standard | Added the following methods: closest() , insertAdjacentElement() and insertAdjacentText() .Moved hasAttributes() from the Node interface to this one. |
DOM4 The definition of 'Element' in that specification. | Obsolete | Removed the following methods: setIdAttribute() , setIdAttributeNS() , and setIdAttributeNode() .Modified the return value of getElementsByTagName() and getElementsByTagNameNS() .Removed the schemaTypeInfo property. |
Document Object Model (DOM) Level 3 Core Specification The definition of 'Element' in that specification. | Obsolete | Added the following methods: setIdAttribute() , setIdAttributeNS() , and setIdAttributeNode() . These methods were never implemented and have been removed in later specifications.Added the schemaTypeInfo property. This property was never implemented and has been removed in later specifications. |
Document Object Model (DOM) Level 2 Core Specification The definition of 'Element' in that specification. | Obsolete | The normalize() method has been moved to Node . |
Document Object Model (DOM) Level 1 Specification The definition of 'Element' in that specification. | Obsolete | Initial definition. |
Browser compatibility
BCD tables only load in the browser
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论