Element

Element 是一个通用性非常强的基类,所有 Document 对象下的对象都继承自它。这个接口描述了所有相同种类的元素所普遍具有的方法和属性。一些接口继承自 Element 并且增加了一些额外功能的接口描述了具体的行为。例如, HTMLElement 接口是所有 HTML 元素的基本接口,而 SVGElement 接口是所有 SVG 元素的基础。大多数功能是在这个类的更深层级(hierarchy)的接口中被进一步制定的。

在 Web 平台的领域以外的语言,比如 XUL,通过 XULElement 接口,同样也实现了 Element 接口。

EventTarget Node Element

属性

*所有属性继承自它的祖先接口 Node,并且扩展了 Node 的父接口 EventTarget,并且从以下部分继承了属性:ParentNodeChildNodeNonDocumentTypeChildNode (en-US),*和 Animatable

Element.attributes 只读

返回一个与该元素相关的所有属性集合 NamedNodeMap

Element.classList 只读

返回该元素包含的 class 属性,是一个 DOMTokenList

Element.className

一个 DOMString,表示这个元素的 class。

Element.clientHeight 只读

返回Number 表示内部相对于外层元素的高度。

Element.clientLeft 只读

返回Number表示该元素距离它左边界的宽度。

Element.clientTop 只读

返回 Number 表示该元素距离它上边界的高度。

Element.clientWidth 只读

返回Number 表示该元素内部的宽度。

Element.computedName 只读

Returns a DOMString containing the label exposed to accessibility.

Element.computedRole 只读

Returns a DOMString containing the ARIA role that has been applied to a particular element.

Element.id

是一个DOMString 表示这个元素的 id。

Element.innerHTML

是一个DOMString 表示这个元素的内容文本。

Element.localName 只读

是一个 DOMString 表示这个元素名称本地化的部分。

Element.namespaceURI 只读

元素对应的 namespace URI,如果没有则返回 null

备注: 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 只读

是一个Element, 该元素下一个兄弟节点,如果为 null 表示不存在..

Element.outerHTML

是一个 DOMString,获取该 DOM 元素及其后代的 HTML 文本。在设置它的时候,会从给定的字符串开始解析,替换自身。

Element.prefix 只读

A DOMString representing the namespace prefix of the element, or null if no prefix is specified.

NonDocumentTypeChildNode.previousElementSibling 只读

是一个 Element, 该元素上一个兄弟节点,如果为 null 表示不存在..

Element.scrollHeight 只读

返回类型为: Number,表示元素的滚动视图高度。

Element.scrollLeft

返回类型为:Number,表示该元素横向滚动条距离最左的位移。

Element.scrollLeftMax 非标准 只读

返回类型为: Number,表示该元素横向滚动条可移动的最大值

Element.scrollTop

返回类型为:Number ,表示该元素纵向滚动条距离

Element.scrollTopMax 非标准 只读

返回类型为:Number ,表示该元素纵向滚动条可移动的最大值

Element.scrollWidth 只读

返回类型为: Number ,表示元素的滚动视图宽度。

Element.shadowRoot只读

Returns the open shadow root that is hosted by the element, or null if no open shadow root is present.

Element.openOrClosedShadowRoot 非标准 只读

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 (en-US) 非标准

Is a Boolean indicating if the element can receive input focus via the tab key.

Element.tagName 只读

Returns a String with the name of the tag for the given element.

Element.undoManager 实验性 只读

Returns the UndoManager associated with the element.

Element.undoScope 实验性

Is a Boolean indicating if the element is an undo scope host, or not.

备注: 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 (en-US) mixin.

Slotable.assignedSlot (en-US)只读

Returns a HTMLSlotElement representing the <slot> the node is inserted in.

事件句柄

Element.onfullscreenchange

事件 fullscreenchange 的回调方法,在元素进入或退出全屏模式时触发。不仅可用于观察(监听)可预期的过度变化,还可以观察(监听)未知的变化,如:当你的应用程序在后台运行。

Element.onfullscreenerror

事件 fullscreenerror 的回调方法,在进入全屏模式过程中出现错误时触发。

方法

Inherits methods from its parents Node, and its own parent, EventTarget, and implements those of ParentNode, ChildNode, NonDocumentTypeChildNode (en-US), and Animatable.

EventTarget.addEventListener()

Registers an event handler to a specific event type on the element.

Element.attachShadow()

Attatches 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 (en-US) on on the element, turning it into a shadow host. Returns a ShadowRoot.

Element.computedStyleMap() (en-US) 实验性

Returns a StylePropertyMapReadOnly (en-US) interface which provides a read-only representation of a CSS declaration block that is an alternative to CSSStyleDeclaration.

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() (en-US) 实验性

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() (en-US)

Retrieves the value of the attribute with the specified name and namespace, from the current node and returns it as an Object.

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.getBoundingClientRect()

返回元素的大小及其相对于视口的位置。

Element.getClientRects()

Returns a collection of rectangles that indicate the bounding rectangles for each line of text in a client.

Element.getElementsByClassName()

参数中给出类的列表,返回一个动态的 HTMLCollection ,包含了所有持有这些类的后代元素。

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() (en-US)

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 (en-US) 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() (en-US)

Releases (stops) pointer capture that was previously set for a specific pointer event.

ChildNode.remove() (en-US) 实验性

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.

Element.removeAttributeNode() 已弃用

Removes the node representation of the named attribute 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.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.

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.

事件

Listen to these events using addEventListener() or by assigning an event listener to the oneventname property of this interface.

cancel (en-US)

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 the oncancel property.

error

Fired when 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 the onerror property.

scroll

Fired when the document view or an element has been scrolled. Also available via the onscroll property.

select

Fired when some text has been selected. Also available via the onselect property.

show

Fired when a contextmenu (en-US) event was fired on/bubbled to an element that has a contextmenu attribute. 已弃用 Also available via the onshow property.

wheel

Fired when the user rotates a wheel button on a pointing device (typically a mouse). Also available via the onwheel property.

剪贴板事件

copy

Fired when the user initiates a copy action through the browser's user interface. Also available via the oncopy property.

cut

Fired when the user initiates a cut action through the browser's user interface. Also available via the oncut property.

paste

Fired when the user initiates a paste action through the browser's user interface. Also available via the onpaste 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 the onblur property.

focus

Fired when an element has gained focus. Also available via the onfocus property

focusin (en-US)

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 the onfullscreenchange 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 the onfullscreenerror property.

键盘事件

keydown

Fired when a key is pressed. Also available via the onkeydown property.

keypress

Fired when a key that produces a character value is pressed down. 已弃用 Also available via the onkeypress property.

keyup

Fired when a key is released. Also available via the onkeyup property.

鼠标事件

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 the onauxclick 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 the onclick property.

contextmenu

Fired when the user attempts to open a context menu. Also available via the oncontextmenu 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 the ondblclick 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 the onmousedown property.

mouseenter

Fired when a pointing device (usually a mouse) is moved over the element that has the listener attached. Also available via the onmouseenter 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 the onmouseleave property.

mousemove

Fired when a pointing device (usually a mouse) is moved while over an element. Also available via the onmousemove 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 the onmouseout 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 the onmouseover property.

mouseup

Fired when a pointing device button is released on an element. Also available via the onmouseup property.

webkitmouseforcechanged (en-US)

Fired each time the amount of pressure changes on the trackpadtouchscreen.

webkitmouseforcedown (en-US)

Fired after the mousedown event as soon as sufficient pressure has been applied to qualify as a "force click".

webkitmouseforcewillbegin (en-US)

Fired before the mousedown event.

webkitmouseforceup (en-US)

Fired after the webkitmouseforcedown (en-US) 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 the ontouchcancel property.

touchend

Fired when one or more touch points are removed from the touch surface. Also available via the ontouchend (en-US) property

touchmove

Fired when one or more touch points are moved along the touch surface. Also available via the ontouchmove property

touchstart

Fired when one or more touch points are placed on the touch surface. Also available via the ontouchstart property

规范

Specification
DOM Standard
# interface-element
Pointer Events
# extensions-to-the-element-interface
Pointer Lock 2.0
# extensions-to-the-element-interface
Fullscreen API Standard
# api
DOM Parsing and Serialization
# extensions-to-the-element-interface
CSSOM View Module
# extension-to-the-element-interface

浏览器兼容性

BCD tables only load in the browser