Disable Text Field Autofocus in Javascript

瘦欲@ 提交于 2019-12-07 03:04:00

问题


Is there a way in Javascript to forcibly disable text field autofocus on page load?

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.

Thanks!


回答1:


You can use .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();
    });
}



回答2:


You can use in jQuery

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



回答3:


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.




回答4:


You could force focus on an object higher in the page on load.



来源:https://stackoverflow.com/questions/6765681/disable-text-field-autofocus-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!