getAttributeNS()
方法在
元素
interface returns the string value of the attribute with the specified namespace and name. If the named attribute does not exist, the value returned will either be
null
or
""
(the empty string); see
注意事项
了解细节。
attrVal = element.getAttributeNS(namespace, name)
namespace
The namespace in which to look for the specified attribute.
名称
The name of the attribute to look for.
The string value of the specified attribute. If the attribute doesn't exist, the result is
null
.
The following SVG document reads the value of the
foo
attribute in a custom namespace.
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:test="http://www.example.com/2014/test" width="40" height="40">
<circle id="target" cx="12" cy="12" r="10" stroke="#444"
stroke-width="2" fill="none" test:foo="Hello namespaced attribute!"/>
<script type="text/javascript">
var ns = 'http://www.example.com/2014/test';
var circle = document.getElementById( 'target' );
console.log( 'attribute test:foo: "' + circle.getAttributeNS( ns, 'foo' ) + '"' );
</script>
</svg>
In an HTML5 document the attribute has to be accessed with
test:foo
since namespaces are not supported.
<!DOCTYPE html>
<html>
<body>
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:test="http://www.example.com/2014/test" width="40" height="40">
<circle id="target" cx="12" cy="12" r="10" stroke="#444" stroke-width="2"
fill="none" test:foo="Foo value"/>
</svg>
<script type="text/javascript">
var ns = 'http://www.example.com/2014/test';
var circle = document.getElementById( 'target' );
console.log('Attribute value: ' + circle.getAttribute('test:foo'));
</script>
</body>
</html>
Namespaces are only supported in XML documents. HTML5 documents have to use
getAttribute()
代替。
getAttributeNS()
不同于
getAttribute()
in that it allows you to further specify the requested attribute as being part of a particular namespace, as in the example above, where the attribute is part of the fictional "specialspace" namespace on Mozilla.
Prior to the DOM4 specification, this method was specified to return an empty string rather than null for non-existent attributes. However, most browsers instead returned null. Starting with DOM4, the specification now says to return null. However, some older browsers return an empty string. For that reason, you should use
hasAttributeNS()
to check for an attribute's existence prior to calling
getAttributeNS()
if it is possible that the requested attribute does not exist on the specified element.
DOM methods dealing with element's attributes:
| Not namespace-aware, most commonly used methods | Namespace-aware variants (DOM Level 2) |
DOM Level 1 methods for dealing with
Attr
nodes directly (seldom used)
|
DOM Level 2 namespace-aware methods for dealing with
Attr
nodes directly (seldom used)
|
|---|---|---|---|
setAttribute
(DOM 1)
|
setAttributeNS
|
setAttributeNode
|
setAttributeNodeNS
|
getAttribute
(DOM 1)
|
getAttributeNS
|
getAttributeNode
|
getAttributeNodeNS
|
hasAttribute
(DOM 2)
|
hasAttributeNS
|
- | - |
removeAttribute
(DOM 1)
|
removeAttributeNS
|
removeAttributeNode
|
- |
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'Element.getAttributeNS()' in that specification. |
实时标准 | |
|
DOM (文档对象模型) 3 级核心规范
The definition of 'Element.getAttributeNS()' in that specification. |
过时 |
Specifies that a
NOT_SUPPORTED_ERR
exception is thrown if the UA does not support the
"XML"
feature. Also specifies that
null
must be passed to have no namespace.
|
|
DOM (文档对象模型) 级别 2 核心规范
The definition of 'Element.getAttributeNS()' in that specification. |
过时 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
getAttributeNS
|
Chrome Yes | Edge 12 |
Firefox
Yes
|
IE Yes | Opera Yes | Safari 6 | 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