HTMLCollection
interface represents a generic collection (array-like object similar to
arguments
) of elements (in document order) and offers methods and properties for selecting from the list.
HTMLCollection
for historical reasons (before the modern DOM, collections implementing this interface could only have HTML elements as their items).
An
HTMLCollection
in the HTML DOM is live; it is automatically updated when the underlying document is changed.
HTMLCollection.length
只读
Returns the number of items in the collection.
HTMLCollection.item()
index
into the list. Returns
null
若
index
超出范围。
collection[
i
]
(which instead returns
undefined
当
i
is out-of-bounds). This is mostly useful for non-JavaScript DOM implementations.
HTMLCollection.namedItem()
名称
. Matching by name is only done as a last resort, only in HTML, and only if the referenced element supports the
名称
attribute. Returns
null
if no node exists by the given name.
collection[
名称
]
(which instead returns
undefined
当
名称
does not exist). This is mostly useful for non-JavaScript DOM implementations.
HTMLCollection
also exposes its members directly as properties by both name and index. HTML IDs may contain
:
and
.
as valid characters, which would necessitate using bracket notation for property access. Currently
HTMLCollections
does not recognize purely numeric IDs, which would cause conflict with the array-style access, though HTML5 does permit these.
For example, assuming there is one
<form>
element in the document and its
id
is
myForm
:
var elem1, elem2;
// document.forms is an HTMLCollection
elem1 = document.forms[0];
elem2 = document.forms.item(0);
alert(elem1 === elem2); // shows: "true"
elem1 = document.forms.myForm;
elem2 = document.forms.namedItem("myForm");
alert(elem1 === elem2); // shows: "true"
elem1 = document.forms["named.item.with.periods"];
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'HTMLCollection' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
HTMLCollection
|
Chrome 1 | Edge 12 | Firefox 1 | IE 8 | Opera 8 | Safari 4 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 3.2 | Samsung Internet Android 1.0 |
item
|
Chrome Yes | Edge 12 | Firefox Yes | IE ? | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android Yes | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
length
|
Chrome Yes | Edge 12 | Firefox Yes | IE ? | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android Yes | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
namedItem
|
Chrome Yes | Edge 12 | Firefox Yes | IE Yes | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android Yes | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
完整支持
兼容性未知
HTMLCollection
AbortController
AbortSignal
AbstractRange
Attr
ByteString
CDATASection
CSSPrimitiveValue
CSSValue
CSSValueList
CharacterData
ChildNode
注释
CustomEvent
DOMConfiguration
DOMError
DOMErrorHandler
DOMException
DOMImplementation
DOMImplementationList
DOMImplementationRegistry
DOMImplementationSource
DOMLocator
DOMObject
DOMParser
DOMPoint
DOMPointInit
DOMPointReadOnly
DOMRect
DOMString
DOMTimeStamp
DOMTokenList
DOMUserData
Document
DocumentFragment
DocumentType
元素
ElementTraversal
Entity
EntityReference
事件
EventTarget
MutationObserver
节点
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
范围
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument