在JavaScript中禁用文本字段自动对焦

在JavaScript中禁用文本字段自动对焦

本文介绍了在JavaScript中禁用文本字段自动对焦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Javascript中是否有一种方法可以在页面加载时强制禁用文本字段自动对焦?

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

我知道focus()方法,但不知道如何禁用它.我的实际问题是,在模式窗口中自动对焦会迫使该窗口在滚动条已经向下一半的情况下出现并滚动到第一个输入字段,而我想完全禁用它.

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.

谢谢!

推荐答案

您可以使用.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();
    });
}

这篇关于在JavaScript中禁用文本字段自动对焦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 05:03