From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/window/blur_event/index.html | 116 +++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 files/zh-cn/web/api/window/blur_event/index.html (limited to 'files/zh-cn/web/api/window/blur_event/index.html') diff --git a/files/zh-cn/web/api/window/blur_event/index.html b/files/zh-cn/web/api/window/blur_event/index.html new file mode 100644 index 0000000000..44cc5ffd7f --- /dev/null +++ b/files/zh-cn/web/api/window/blur_event/index.html @@ -0,0 +1,116 @@ +--- +title: 'Window: blur event' +slug: Web/API/Window/blur_event +translation_of: Web/API/Window/blur_event +--- +
{{APIRef}}
+ +

当元素失去焦点时,blur事件将触发。

+ +

与 blur 相反的是{{domxref("Window/focus_event", "focus")}}。

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Bubbles(是否支持冒泡)No
Cancelable(可撤销)No
Interface(接口){{DOMxRef("FocusEvent")}}
Event handler property(事件处理程序属性){{domxref("GlobalEventHandlers/onblur", "onblur")}}
Sync / Async(同步/异步)Sync
ComposedYes
+ +

示例

+ +

在线示例

+ +

此示例当文档失去焦点时,更改其外观。它使用{{domxref("EventTarget.addEventListener()","addEventListener()")}} 去监听{{domxref("Window/focus_event", "focus")}}和 blur 事件。

+ +

HTML

+ +
<p id="log">Click on this document to give it focus.</p>
+ +

CSS

+ +
.paused {
+  background: #ddd;
+  color: #555;
+}
+ +

JavaScript

+ +
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("在线示例")}}

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{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。

+ +

参见

+ + -- cgit v1.2.3-54-g00ecf