HTMLElement: blur() 方法

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

HTMLElement.blur() 方法会移除当前元素的键盘焦点。

语法

js
blur()

参数

无。

返回值

无(undefined)。

示例

移除文本输入框的焦点

HTML

html
<input type="text" id="sampleText" value="Sample Text" /><br /><br />
<button type="button">Click me to gain focus</button>

JavaScript

js
const textField = document.getElementById("sampleText");
const button = document.querySelector("button");

function focusInput() {
  textField.focus();

  // The input will lose focus after 3 seconds
  setTimeout(() => {
    textField.blur();
  }, 3000);
}

button.addEventListener("click", focusInput);

结果

规范

规范
HTML
# dom-blur-dev

浏览器兼容性

另见