JavaScript setFocus()函数不起作用 [英] JavaScript setFocus() function is not working

查看:86
本文介绍了JavaScript setFocus()函数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus."



当我尝试从代码动态聚焦HTML输入时,将显示此JavaScript错误.该控件是HTML输入,它既不可见也不被禁用.



This JavaScript error is shown when I try to focusing an HTML input dynamically from code... the control is HTML input and it is neither invisible nor disabled.

What could be the reason of the error and what is the solution for it?

推荐答案

检查html输入控件的父元素是否被禁用.如果您的输入元素带有已禁用的div,那么您将无法专注于该输入控件.
Check whether the parent element of the html input control is disabled or not. If your input element is withing a div that is disabled then you cannot focus to that input control.


进行这样的检查,看看提供的原因是否真实:
Put a check like this and see if the reasons provided are really true:
if(element.style.visibility=="hidden" || element.style.display=="none" || element.disabled==true) {
    //do something because it can't accept focus()
} else {
    element.focus();
}



如果仍然出现错误,请使用"try-catch"块并进行验证.



If you still get the error, then use a ''try-catch'' block and verify the same.


这篇关于JavaScript setFocus()函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆