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 |
浏览器兼容性
加载中…