简体   繁体   中英

Is it possible to remove the focus from a text input when a page loads?

我有一个网站有一个文本字段用于搜索,但我不希望焦点在页面加载时,但它是表格上唯一的文本输入,是否可以删除焦点?

A jQuery solution would be something like:

$(function () {
    $('input').blur();
});

use document.activeElement.blur();

example at http://jsfiddle.net/vGGdV/5/ that shows the currently focused element as well.

Keep a note though that calling blur() on the body element in IE will make the IE lose focus

You can use the .blur() method. See http://api.jquery.com/blur/

$(function() {
 $("#MyTextBox").blur();
});

I would add that HTMLElement has a built-in .blur method as well.

Here's a demo using both .focus and .blur which work in similar ways.

 const input = document.querySelector("#myInput"); 
 <input id="myInput" value="Some Input"> <button type="button" onclick="input.focus()">Focus</button> <button type="button" onclick="input.blur()">Lose focus</button> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM