返回属于给定名称空间采用给定标签名称的元素列表。搜索完整文档,包括根节点。
elements = document.getElementsByTagNameNS(namespace, name)
NodeList
(但请参阅以下注意事项) 找到的元素按照它们在树中出现的次序排列。
element.namespaceURI
).
*
, which matches all elements (see
element.localName
).
注意:
While the W3C specification says
元素
是
NodeList
, this method returns a
HTMLCollection
both in Gecko and Internet Explorer. Opera returns a
NodeList
, but with a
namedItem
method implemented, which makes it similar to a
HTMLCollection
. As of January 2012, only in WebKit browsers is the returned value a pure
NodeList
。见
bug 14869
了解细节。
注意:
Currently parameters in this method are case-sensitive, but they were case-insensitive in Firefox 3.5 and before. See the
developer release note for Firefox 3.6
and a note in Browser compatibility section in
Element.getElementsByTagNameNS
了解细节。
In the following example
getElementsByTagNameNS
starts from a particular parent element, and searches topdown recursively through the DOM from that parent element, looking for child elements matching the tag
名称
参数。
Note that when the node on which
getElementsByTagName
is invoked is not the
document
node, in fact the
element.getElementsByTagNameNS
method is used.
To use the following example, just copy/paste it into a new file saved with the .xhtml extension.
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>getElementsByTagNameNS example</title> <script type="text/javascript"> function getAllParaElems() { var allParas = document.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "p"); var num = allParas.length; alert("There are " + num + " <p> elements in this document"); } function div1ParaElems() { var div1 = document.getElementById("div1") var div1Paras = div1.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "p"); var num = div1Paras.length; alert("There are " + num + " <p> elements in div1 element"); } function div2ParaElems() { var div2 = document.getElementById("div2") var div2Paras = div2.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "p"); var num = div2Paras.length; alert("There are " + num + " <p> elements in div2 element"); } </script> </head> <body style="border: solid green 3px"> <p>Some outer text</p> <p>Some outer text</p> <div id="div1" style="border: solid blue 3px"> <p>Some div1 text</p> <p>Some div1 text</p> <p>Some div1 text</p> <div id="div2" style="border: solid red 3px"> <p>Some div2 text</p> <p>Some div2 text</p> </div> </div> <p>Some outer text</p> <p>Some outer text</p> <button onclick="getAllParaElems();"> show all p elements in document</button><br /> <button onclick="div1ParaElems();"> show all p elements in div1 element</button><br /> <button onclick="div2ParaElems();"> show all p elements in div2 element</button> </body> </html>
If the desired browser did not support XPath, another approach (such as traversing the DOM through all its children, identifying all @xmlns instances, etc.) would be necessary to find all tags with the desired local name and namespace, but XPath is much faster. (To accommodate Explorer, one could call an XPath wrapper instead of the XPath in the function below (as Explorer supports XPath with a different API), such as this wrapper class )。
function getElementsByTagNameNSWrapper (ns, elName, doc, context) {
if (!doc) {
doc = document;
}
if (!context) {
context = doc;
}
var result = doc.evaluate('//*[local-name()="'+elName+'" and namespace-uri() = "'+ns+'"]', context, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
var a = [];
for(var i = 0; i < result.snapshotLength; i++) {
a[i] = result.snapshotItem(i);
}
return a;
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'document.getElementsByTagNameNS' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
getElementsByTagNameNS
|
Chrome 1 | 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 |
完整支持
兼容性未知
Document
alinkColor
all
anchors
applets
bgColor
body
characterSet
childElementCount
children
compatMode
contentType
currentScript
defaultView
designMode
dir
doctype
documentElement
documentURI
documentURIObject
domain
domConfig
嵌入
fgColor
firstElementChild
forms
fullscreen
fullscreenEnabled
head
height
hidden
图像
实现
lastElementChild
lastModified
lastStyleSheetSet
linkColor
链接
location
mozSyntheticDocument
onabort
onafterscriptexecute
onanimationcancel
onanimationend
onanimationiteration
onauxclick
onbeforescriptexecute
onblur
oncancel
oncanplay
oncanplaythrough
onchange
onclick
onclose
oncontextmenu
oncuechange
ondblclick
ondurationchange
onended
onerror
onfocus
onformdata
onfullscreenchange
onfullscreenerror
ongotpointercapture
oninput
oninvalid
onkeydown
onkeypress
onkeyup
onload
onloadeddata
onloadedmetadata
onloadend
onloadstart
onlostpointercapture
onmousedown
onmouseenter
onmouseleave
onmousemove
onmouseout
onmouseover
onmouseup
onoffline
ononline
onpause
onplay
onplaying
onpointercancel
onpointerdown
onpointerenter
onpointerleave
onpointermove
onpointerout
onpointerover
onpointerup
onreset
onresize
onscroll
onselect
onselectionchange
onselectstart
onsubmit
ontouchcancel
ontouchstart
ontransitioncancel
ontransitionend
onvisibilitychange
onwheel
origin
plugins
popupNode
preferredStyleSheetSet
readyState
referrer
rootElement
脚本
scrollingElement
selectedStyleSheetSet
styleSheetSets
timeline
title
tooltipNode
URL
visibilityState
vlinkColor
width
xmlEncoding
xmlVersion
adoptNode()
append()
caretRangeFromPoint()
clear()
close()
createAttribute()
createCDATASection()
createComment()
createDocumentFragment()
createElement()
createElementNS()
createEntityReference()
createEvent()
createExpression()
createExpression()
createNodeIterator()
createNSResolver()
createNSResolver()
createProcessingInstruction()
createRange()
createTextNode()
createTouch()
createTouchList()
createTreeWalker()
enableStyleSheetsForSet()
evaluate()
evaluate()
execCommand()
exitFullscreen()
exitPointerLock()
getAnimations()
getBoxObjectFor()
getElementById()
getElementsByClassName()
getElementsByName()
getElementsByTagName()
getElementsByTagNameNS()
hasFocus()
hasStorageAccess()
importNode()
mozSetImageElement()
open()
prepend()
queryCommandEnabled()
queryCommandSupported()
querySelector()
querySelector()
querySelectorAll()
querySelectorAll()
registerElement()
releaseCapture()
replaceChildren()
requestStorageAccess()
write()
writeln()
animationcancel
animationend
animationiteration
animationstart
copy
cut
DOMContentLoaded
drag
dragend
dragenter
dragexit
dragleave
dragover
dragstart
drop
fullscreenchange
fullscreenerror
gotpointercapture
keydown
keypress
keyup
lostpointercapture
paste
pointercancel
pointerdown
pointerenter
pointerleave
pointerlockchange
pointerlockerror
pointermove
pointerout
pointerover
pointerup
readystatechange
scroll
selectionchange
selectstart
touchcancel
touchend
touchmove
touchstart
transitioncancel
transitionend
transitionrun
transitionstart
visibilitychange
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
DocumentFragment
DocumentType
元素
ElementTraversal
Entity
EntityReference
事件
EventTarget
HTMLCollection
MutationObserver
节点
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
范围
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument