HTMLElement: blur() 方法
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 |
瀏覽器相容性
載入中…