getAttribute()
方法在
元素
interface returns the value of a specified attribute on the element.
If the given attribute does not exist, the value returned will either be
null
or
""
(the empty string); see
Non-existing attributes
了解细节。
let attribute = element.getAttribute(attributeName);
where
属性
is a string containing the value of
attributeName
.
attributeName
is the name of the attribute whose value you want to get.
const div1 = document.getElementById('div1');
const align = div1.getAttribute('align');
alert(align); // Shows the value of align for the element with id="div1"
When called on an HTML element in a DOM flagged as an HTML document,
getAttribute()
lower-cases its argument before proceeding.
Essentially all web browsers (Firefox, Internet Explorer, recent versions of Opera, Safari, Konqueror, and iCab, as a non-exhaustive list) return
null
when the specified attribute does not exist on the specified element; this is what
the current DOM specification draft
specifies. The old DOM 3 Core specification, on the other hand, says that the correct return value in this case is actually the
空字符串
, and some DOM implementations implement this behavior. The implementation of
getAttribute()
in XUL (Gecko) actually follows the DOM 3 Core specification and returns an empty string. Consequently, you should use
element.hasAttribute()
to check for an attribute's existence prior to calling
getAttribute()
if it is possible that the requested attribute does not exist on the specified element.
For security reasons,
CSP
nonces from non-script sources, such as CSS selectors, and
.getAttribute("nonce")
calls are hidden.
let nonce = script.getAttribute('nonce');
// returns empty string
Instead of retrieving the nonce from the content attribute, use the
nonce
特性:
let nonce = script.nonce;
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'getAttribute()' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
getAttribute
|
Chrome 1 | Edge 12 | Firefox 23 | IE 8 | Opera 8 | Safari 1.3 | WebView Android 1 | Chrome Android 18 | Firefox Android 23 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 |
完整支持
元素
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