Disable Text Field Autofocus in Javascript

2019-04-28 15:35发布

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!

4条回答
三岁会撩人
2楼-- · 2019-04-28 16:15

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

查看更多
\"骚年 ilove
3楼-- · 2019-04-28 16:24

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.

查看更多
Bombasti
4楼-- · 2019-04-28 16:25

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();
    });
}
查看更多
做个烂人
5楼-- · 2019-04-28 16:25

You can use in jQuery

$(function(){
    $('input').blur();
});
查看更多
登录 后发表回答