toggleAttribute()
方法在
元素
interface toggles a Boolean attribute (removing it if it is present and adding it if it is not present) on the given element.
Element.toggleAttribute(name [, force]);
名称
DOMString
specifying the name of the attribute to be toggled. The attribute name is automatically converted to all lower-case when
toggleAttribute()
is called on an HTML element in an HTML document.
force
可选
A boolean value to determine whether the attribute should be added or removed, no matter whether the attribute is present or not at the moment.
true
若属性
名称
is eventually present, and
false
否则。
InvalidCharacterError
名称
contains one or more characters which are not valid in attribute names.
在以下范例中,
toggleAttribute()
is used to toggle the
readonly
attribute of a
<input>
.
<input value="text">
<button>toggleAttribute("readonly")</button>
var button = document.querySelector("button");
var input = document.querySelector("input");
button.addEventListener("click", function(){
input.toggleAttribute("readonly");
});
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
|
- |
if (!Element.prototype.toggleAttribute) {
Element.prototype.toggleAttribute = function(name, force) {
if(force !== void 0) force = !!force
if (this.hasAttribute(name)) {
if (force) return true;
this.removeAttribute(name);
return false;
}
if (force === false) return false;
this.setAttribute(name, "");
return true;
};
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'Element.toggleAttribute' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
toggleAttribute
|
Chrome 69 | Edge 18 | Firefox 63 | IE No | Opera 56 | Safari 12 | WebView Android 69 | Chrome Android 69 | Firefox Android 63 | Opera Android 48 | Safari iOS Yes | Samsung Internet Android 10.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