--- title: 'Window: blur event' slug: Web/API/Window/blur_event translation_of: Web/API/Window/blur_event ---
当元素失去焦点时,blur事件将触发。
与 blur 相反的是{{domxref("Window/focus_event", "focus")}}。
| Bubbles(是否支持冒泡) | No |
|---|---|
| Cancelable(可撤销) | No |
| Interface(接口) | {{DOMxRef("FocusEvent")}} |
| Event handler property(事件处理程序属性) | {{domxref("GlobalEventHandlers/onblur", "onblur")}} |
| Sync / Async(同步/异步) | Sync |
| Composed | Yes |
此示例当文档失去焦点时,更改其外观。它使用{{domxref("EventTarget.addEventListener()","addEventListener()")}} 去监听{{domxref("Window/focus_event", "focus")}}和 blur 事件。
<p id="log">Click on this document to give it focus.</p>
.paused {
background: #ddd;
color: #555;
}
function pause() {
document.body.classList.add('paused');
log.textContent = 'FOCUS LOST!';
}
function play() {
document.body.classList.remove('paused');
log.textContent = 'This document has focus. Click outside the document to lose focus.';
}
const log = document.getElementById('log');
window.addEventListener('blur', pause);
window.addEventListener('focus', play);
{{EmbedLiveSample("在线示例")}}
| Specification | Status | Comment |
|---|---|---|
| {{SpecName("UI Events", "#event-type-blur")}} | {{Spec2("UI Events")}} | Added info that this event is composed. |
| {{SpecName("DOM3 Events", "#event-type-blur")}} | {{Spec2("DOM3 Events")}} | Initial definition |
{{Compat("api.Window.blur_event")}}
{{DOMxRef(" document. activeelement ")}}的值在处理({{bug(452307)}})时因浏览器而异 ;({{bug(452307)}}):IE10将其设置为焦点将移动到的元素,而Firefox和Chrome通常将其设置为文档的 body。