Element(元素)接口是 Document
的一个对象. 这个接口描述了所有相同种类的元素所普遍具有的方法和属性。 这些继承自Element并且增加了一些额外功能的接口描述了具体的行为. 例如, HTMLElement
接口是所有HTML元素的基础接口, 而 SVGElement
接口是所有SVG元素的基本接口.
在web以外的语言,像 XUL 可以通过 XULElement
的API, 也能实现它.
属性
所有属性继承至它的祖先接口 Node
, 和它所扩展的接口 EventTarget
, 并且从以下部分继承了属性ParentNode
, ChildNode
, NonDocumentTypeChildNode
, 和Animatable
.
Element.assignedSlot
只读- 返回元素对应的
HTMLSlotElement
接口
Element.attributes
只读- 返回一个与该元素相关的所有属性集合
NamedNodeMap
Element.classList
只读- 返回该元素包含的class属性是一个
DOMTokenList
. Element.className
- 它是一个
DOMString
表示这个元素的class. Element.clientHeight
只读- 返回
Number
表示内部相对于外层元素的高度. Element.clientLeft
只读- 返回
Number
表示该元素距离它左边界的宽度. Element.clientTop
只读- 返回
Number
表示该元素距离它上边界的高度. Element.clientWidth
只读- 返回
Number
表示该元素它内部的宽度. Element.id
- 是一个
DOMString
表示这个元素的id. Element.innerHTML
- 是一个
DOMString
表示这个元素的内容文本 Element.localName
只读- 是一个
DOMString
表示这个元素名称本地化的部分 ParentNode.lastElementChild
只读- 是一个
Element
, 直接获取该元素下最后一个子元素,如果为null表示不存在. 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文本
.当设置它的时候,会从给定的字符串开始解析,替换自身。 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 youngest shadow root that is hosted by the element.
Element.tabStop
- 返回类型为:
Boolean
, indicating if the element can receive input focus via the tab key. Element.tagName
只读- 返回类型为:
String
,表示该元素的标签名 Element.undoManager
只读- 返回
UndoManager
associated with the element. Element.undoScope
- 类型为:
Boolean
。indicating if the element is an undo scope host, or not.
事件句柄
Element.ongotpointercapture
- Returns the event handler for the
gotpointercapture
event type. Element.onlostpointercapture
- Returns the event handler for the
lostpointercapture
event type. Element.onwheel
- Returns the event handling code for the
wheel
event.
方法
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.closest()
- Returns the
Element
, descendant of this element (or this element itself), that is the closest ancestor of the elements selected by the selectors given in parameter. Element.createShadowRoot()
- Creates a shadow DOM on on the element, turning it into a shadow host. Returns a
ShadowRoot
. EventTarget.dispatchEvent()
- Dispatches an event to this node in the DOM and returns a
Boolean
that indicates that at least one handler has not canceled it. Element.find()
- ...
Element.findAll()
- ...
Animatable.getAnimationPlayers()
- …
Element.getAttribute()
- Retrieves the value of the named attribute from the current node and returns it as an
Object
. 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.getAttributeNode()
- Retrievse 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.getDestinationInsertionPoints()
- …
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.insertAdjacentHTML
- Parses the text as HTML or XML and inserts the resulting nodes into the tree in the position given.
Element.matches()
- Returns a
Boolean
indicating whether or not the element would be selected by the specified selector string. Element.querySelector()
- Returns
Node
... Element.querySelectorAll
- Returns a
NodeList
... 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.
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.scrollIntoView()
- Scrolls the page until the element gets into the view.
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()
- Setw 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
.
规范
规范文档 | 状态 | 说明 |
---|---|---|
Shadow DOM | Editor's Draft | |
Web Animations | Working Draft | Added the getAnimationPlayers() method. |
UndoManager and DOMTransaction Element |
Editor's Draft | Added the undoScope and undoManager properties. |
Pointer Events Element |
Recommendation | Added the following event handlers: ongotpointercapture and onlostpointercapture .Added the following methods: setPointerCapture() and releasePointerCapture() . |
Selectors API Level 2 Element |
Obsolete | Added the following methods: matches() (implemented as mozMatchesSelector() ), find() , findAll() . |
Selectors API Level 1 Element |
Obsolete | Added the following methods: querySelector() and querySelectorAll() . |
Pointer Lock Element |
Candidate Recommendation | Added the requestPointerLock() method. |
Fullscreen API Element |
Living Standard | Added the requestFullscreen() method. |
DOM Parsing and Serialization Element |
Working Draft | Added the following properties: innerHTML , and outerHTML .Added the following method: insertAdjacentHTML() . |
CSS Object Model (CSSOM) View Module Element |
Working Draft | Added the following properties: scrollTop , scrollLeft , scrollWidth , scrollHeight , clientTop , clientLeft , clientWidth , and clientHeight .Added the following methods: getClientRects() , getBoundingClientRect() , and scrollIntoView() . |
Element Traversal Specification Element |
Obsolete | Added inheritance of the ElementTraversal interface. |
DOM Element |
Living Standard | Removed the following methods: closest() , setIdAttribute() , setIdAttributeNS() , and setIdAttributeNode() .Removed the schemaTypeInfo property.Modified the return value of getElementsByTag() and getElementsByTagNS() .Moved hasAttributes() form the Node interface to this one. |
Document Object Model (DOM) Level 3 Core Specification Element |
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 Element |
Obsolete | The normalize() method has been moved to Node . |
Document Object Model (DOM) Level 1 Specification Element |
Obsolete | Initial definition. |
浏览器兼容性
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 1.0 | 1.0 (1.7 or earlier) | (Yes) | (Yes) | 1.0 |
children |
(Yes) | 3.0 (1.9) | 7.0 with a significant bug [1] 9.0 according to the spec |
(Yes) | 未实现 |
childElementCount , nextElementSibling , previousElementSibling |
(Yes) | 3.5 (1.9.1) | 9.0 | (Yes) | (Yes) |
firstElementChild , lastElementChild |
(Yes) | 3.0 (1.9) | 9.0 | (Yes) | (Yes) |
classList |
(Yes) | 3.6 (1.9.2) | (Yes) | (Yes) | |
outerHTML |
(Yes) | 11 (11) | (Yes) | (Yes) | (Yes) |
clientLeft , clientTop |
(Yes) | 3.5 (1.9.1) | (Yes) | (Yes) | (Yes) |
getBoundingClientRect() , getClientRects() |
(Yes) | 3.0 (1.9) | (Yes) | (Yes) | (Yes) |
querySelector() , querySelectorAll() |
1.0 | 3.5 (1.9.1) | 8.0 | 10.0 | 3.2 (525.3) |
insertAdjacentHTML() |
1.0 | 8 (8) | 4.0 | 7.0 | 4.0 (527) |
setCapture() |
未实现 | 4.0 (2) | 未实现 | 未实现 | 未实现 |
oncopy , oncut , onpaste |
未实现 | 3.0 (1.9) | (Yes) | 未实现 | |
onwheel |
未实现 | 17 (17) | 未实现 | 未实现 | 未实现 |
ongotpointercapture , onlostpointercapture , setPointerCapture() , and releasePointerCapture() |
未实现 | 未实现 | 10.0 MS | 未实现 | 未实现 |
matches() |
(Yes) with the non-standard name webkitMatchesSelector |
3.6 (1.9.2) with the non-standard name mozMatchesSelector 34 (34) with the standard name |
9.0 with the non-standard name msMatchesSelector |
11.5 with the non-standard name oMatchesSelector 15.0 with the non-standard name webkitMatchesSelector |
5.0 with the non-standard name webkitMatchesSelector |
find() and findAll() |
未实现 | 未实现 | 未实现 | 未实现 | 未实现 |
requestPointerLock() |
16.0 webkit, behind an about:flags 22.0 webkit (with special cases, progressively lifted see [2]) |
14 (14)moz | 未实现 | 未实现 | 未实现 |
requestFullscreen() |
14.0 webkit | 10 (10) moz | 11.0 ms | 12.10 15.0 webkit |
5.1 webkit |
undoManager and undoScope |
未实现 | (Yes) (behind the dom.undo_manager.enabled pref) |
未实现 | 未实现 | 未实现 |
attributes |
? | 22 (22) Before this it was available via the Node interface that any element inherits. |
? | ? | ? |
scrollTopMax() and scrollLeftMax() |
未实现 | 16 (16) | 未实现 | 未实现 | 未实现 |
closest() |
? | 35 (35) | ? | ? | ? |
hasAttributes() |
(Yes) | 1.0 (1.7 or earlier) (on the Node interface)35 (35) (on this interface |
(Yes) | (Yes) | (Yes) |
Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | 1.0 | 1.0 (1) | (Yes) | (Yes) | 1.0 |
scrollTopMax() and scrollLeftMax() |
未实现 | 16.0 (16) | 未实现 | 未实现 | 未实现 |
closest() |
? | 35.0 (35) | ? | ? | ? |
hasAttributes() |
(Yes) | 1.0 (1.0) (on the Node interface)35.0 (35) (on this interface |
(Yes) | (Yes) | (Yes) |
[1] Internet Explorer 7 and 8 incorrectly return the comments as part of the children of an Element. This is fixed in Internet Explorer 9 and later.
[2] Chrome 16 allowed webkitRequestPointerLock()
only in fullscreen; Chrome 21 for trusted web site (permission asked); Chrome 22 allowed it by default for all same-origin document; Chrome 23 allowed it in sandboxed <iframe>
if the non-standard value webkit-allow-pointer-lock
is set to the sandbox
attribute.