javascript Greybox:无法将焦点移到控件上,因为它不可见、未启用或属于不接受焦点的类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3452281/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:13:09  来源:igfitidea点击:

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

javascript

提问by Michael

Possible Duplicate:
JavaScript: Visibility error in Internet Explorer when setting focus on an input element

可能的重复:
JavaScript:将焦点设置在输入元素上时 Internet Explorer 中的可见性错误

I have a page that loads within a greybox. I set the focus with document.getElementById("textfield").focus()- this works fine when calling the page directly.

我有一个在灰盒中加载的页面。我将焦点设置为document.getElementById("textfield").focus()- 这在直接调用页面时工作正常。

But when loaded in a greybox, setting the focus on the onload() event returns:

但是当加载到灰盒中时,将焦点设置在 onload() 事件上会返回:

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

无法将焦点移到控件上,因为它不可见、未启用或属于不接受焦点的类型

Calling it later works fine.

稍后调用它可以正常工作。

Any ideas?

有任何想法吗?

Thanks!

谢谢!

回答by Dor Cohen

It's a well known issue on IE.

这是 IE 上的一个众所周知的问题。

You can read about it here.

你可以在这里阅读它。

The solution is to use the setTimeout()function to delay the focus() execute time.

解决方法是使用setTimeout()函数来延迟 focus() 执行时间。

you need to replace your line:

你需要更换你的线路:

document.getElementById("textfield").focus();

with the following:

具有以下内容:

setTimeout(function() { document.getElementById("textfield").focus(); }, 10);

回答by Autosoft

Just posting a quick answer to this... had to solve this tonight. Used a setTimeout to call the focus function briefly after the greybox page displays.

只是发布一个快速答案......今晚必须解决这个问题。使用 setTimeout 在灰盒页面显示后短暂调用焦点函数。

A little jQuery used in my version since it was already in this project but you could just as easily use window.onload()

在我的版本中使用了一些 jQuery,因为它已经在这个项目中了,但是你可以很容易地使用 window.onload()

<script type="text/javascript">
  $(document).ready(function() {
    setTimeout ( "document.getElementById('AdminID').focus(); ", 500 ); 
  });
</script>