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

瀏覽器相容性

另見