Element.getElementsByTagName()
method returns a live
HTMLCollection
of elements with the given
tag name
.
All descendants of the specified element are searched, but not the element itself. The returned list is
live
, which means it updates itself with the DOM tree automatically. Therefore, there is no need to call
Element.getElementsByTagName()
with the same element and arguments repeatedly if the DOM changes in between calls.
When called on an HTML element in an HTML document,
getElementsByTagName
lower-cases the argument before searching for it. This is undesirable when trying to match camel-cased SVG elements (such as
<linearGradient>
) in an HTML document. Instead, use
Element.getElementsByTagNameNS()
, which preserves the capitalization of the tag name.
Element.getElementsByTagName
类似于
Document.getElementsByTagName()
, except that it only searches for elements that are descendants of the specified element.
elements = element.getElementsByTagName(tagName)
元素
是
live
HTMLCollection
of elements with a matching tag name, in the order they appear. If no elements are found, the
HTMLCollection
is empty.
element
is the element from where the search starts. Only the element's descendants are included, not the element itself.
tagName
is the qualified name to look for. The special string
"*"
represents all elements. For compatibility with XHTML, lower-case should be used.
// Check the status of each data cell in a table
const table = document.getElementById('forecast-table');
const cells = table.getElementsByTagName('td');
for (let cell of cells) {
let status = cell.getAttribute('data-status');
if (status === 'open') {
// Grab the data
}
}
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
getElementsByTagName
|
Chrome
1
|
Edge 12 |
Firefox
1
|
IE 5.5 |
Opera
8
|
Safari
6
|
WebView Android
1
|
Chrome Android
18
|
Firefox Android
4
|
Opera Android 10.1 |
Safari iOS
6
|
Samsung Internet Android
1.0
|
| getElementsByTagName(*) | Chrome 1 | Edge 12 | Firefox Yes | IE 6 | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes |
Firefox Android
Yes
|
Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
完整支持
见实现注意事项。
元素
accessKey
属性
childElementCount
children
classList
className
clientHeight
clientLeft
clientTop
clientWidth
currentStyle
firstElementChild
id
innerHTML
lastElementChild
localName
名称
namespaceURI
nextElementSibling
onfullscreenchange
onfullscreenerror
openOrClosedShadowRoot
outerHTML
part
prefix
previousElementSibling
runtimeStyle
scrollHeight
scrollLeft
scrollLeftMax
scrollTop
scrollTopMax
scrollWidth
shadowRoot
slot
tabStop
tagName
after()
animate()
append()
attachShadow()
before()
closest()
computedStyleMap()
createShadowRoot()
getAnimations()
getAttribute()
getAttributeNames()
getAttributeNode()
getAttributeNodeNS()
getAttributeNS()
getBoundingClientRect()
getClientRects()
getElementsByClassName()
getElementsByTagName()
getElementsByTagNameNS()
hasAttribute()
hasAttributeNS()
hasAttributes()
hasPointerCapture()
insertAdjacentElement()
insertAdjacentHTML()
insertAdjacentText()
matches()
msZoomTo()
prepend()
querySelector()
querySelector()
querySelectorAll()
querySelectorAll()
releasePointerCapture()
remove()
removeAttribute()
removeAttributeNode()
removeAttributeNS()
replaceChildren()
replaceWith()
requestFullscreen()
requestPointerLock()
scroll()
scrollBy()
scrollIntoView()
scrollIntoViewIfNeeded()
scrollTo()
setAttribute()
setAttributeNode()
setAttributeNodeNS()
setAttributeNS()
setCapture()
setPointerCapture()
toggleAttribute()
afterscriptexecute
auxclick
blur
click
compositionend
compositionstart
compositionupdate
contextmenu
copy
cut
dblclick
DOMActivate
DOMMouseScroll
error
focus
focusin
focusout
fullscreenchange
fullscreenerror
gesturechange
gestureend
gesturestart
keydown
keypress
keyup
mousedown
mouseenter
mouseleave
mousemove
mouseout
mouseover
mouseup
mousewheel
MozMousePixelScroll
msContentZoom
MSGestureChange
MSGestureEnd
MSGestureHold
MSGestureStart
MSGestureTap
MSInertiaStart
MSManipulationStateChanged
overflow
paste
scroll
select
show
touchcancel
touchend
touchmove
touchstart
underflow
webkitmouseforcechanged
webkitmouseforcedown
webkitmouseforceup
webkitmouseforcewillbegin
wheel
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
HTMLCollection
MutationObserver
节点
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
范围
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument