在 Javascript 中禁用文本字段自动对焦
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6765681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable Text Field Autofocus in Javascript
提问by sichinumi
Is there a way in Javascript to forcibly disable text field autofocus on page load?
Javascript 中有没有办法在页面加载时强制禁用文本字段自动对焦?
I know of the focus() method, but not how to disable it. My actual problem is that autofocusing in a modal window forces the window to appear with the scrollbar halfway down already and scrolled to the first input field, whereas I would like to disable this entirely.
我知道 focus() 方法,但不知道如何禁用它。我的实际问题是,在模态窗口中自动对焦会强制窗口出现滚动条已经向下一半并滚动到第一个输入字段,而我想完全禁用它。
Thanks!
谢谢!
回答by u283863
You can use .blur()
,
你可以使用.blur()
,
var elelist = document.getElementsByTagName("input");
for(var i = 0; i < elelist.length; i++){
elelist[i].blur();
}
If you want to disable focus on all the textfields,
如果您想禁用对所有文本字段的关注,
var elelist = document.getElementsByTagName("input");
for(var i = 0; i < elelist.length; i++){
elelist[i].addEventListener("focus", function(){
this.blur();
});
}
回答by Warface
You can use in jQuery
您可以在 jQuery 中使用
$(function(){
$('input').blur();
});
回答by Shawn Steward
There's something in the Javascript code you're using that's setting the focus to the field, this is not an automatic behavior. Just find that and disable it.
您正在使用的 Javascript 代码中有一些东西将焦点设置在该字段上,这不是自动行为。只需找到并禁用它。
回答by Jonathan M
You could force focus on an object higher in the page on load.
您可以在加载时强制关注页面中较高的对象。