blur
event fires when an element has lost focus. The main difference between this event and
focusout
is that
focusout
bubbles
while
blur
不会。
The opposite of
blur
is
focus
.
| 冒泡 | No |
|---|---|
| 可取消 | No |
| 接口 |
FocusEvent
|
| 事件处理程序特性 |
onblur
|
| Sync / Async | Sync |
| Composed | Yes |
<form id="form"> <input type="text" placeholder="text input"> <input type="password" placeholder="password"> </form>
const password = document.querySelector('input[type="password"]');
password.addEventListener('focus', (event) => {
event.target.style.background = 'pink';
});
password.addEventListener('blur', (event) => {
event.target.style.background = '';
});
There are two ways of implementing event delegation for this event: by using the
focusout
event, or by setting the
useCapture
参数对于
addEventListener()
to
true
.
<form id="form"> <input type="text" placeholder="text input"> <input type="password" placeholder="password"> </form>
const form = document.getElementById('form');
form.addEventListener('focus', (event) => {
event.target.style.background = 'pink';
}, true);
form.addEventListener('blur', (event) => {
event.target.style.background = '';
}, true);
| 规范 | 状态 | 注释 |
|---|---|---|
| UI Events | 工作草案 | Added info that this event is composed. |
| DOM (文档对象模型) 3 级事件规范 | 过时 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
blur
event
|
Chrome 1 | Edge 12 |
Firefox
24
|
IE 9 | Opera 11.6 | Safari 3.1 | WebView Android 1 | Chrome Android 18 |
Firefox Android
24
|
Opera Android 12.1 | Safari iOS 2 | Samsung Internet Android 1.0 |
完整支持
见实现注意事项。
值
Document.activeElement
varies across browsers while this event is being handled (
bug 452307
): IE10 sets it to the element that the focus will move to, while Firefox and Chrome often set it to the
body
of the document.
元素
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