我有这种情况 - 在我们的 ASPX 站点上,使用 DEVExpress,仅在 IE 8(原始版本,而不是 IE 8 模式下的 IE 9)上,登录页面上我们不断出现错误:
Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus.
所以 - 我研究并发现有东西试图聚焦不可见的东西。所以我尝试了这个解决方案——重写 focus() 方法。这是一个简短的 HTML 页面示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>AccuLynx</title>
<script type="text/javascript">
HTMLInputElement.prototype.focusCpy = HTMLInputElement.prototype.focus;
HTMLInputElement.prototype.focus = function () {
if( this.style.display != 'none' && this.style.visibility != 'hidden' && !this.disabled && this.type != 'hidden' ) {
HTMLInputElement.prototype.focusCpy.apply(this, arguments);
}
}
</script>
</head>
<body>
<input type="submit" id="my_link" style="display:none;" value="dfsdfd" />
<script type="text/javascript">
document.getElementById('my_link').focus();
</script>
</body>
</html>
然后在测试页面上一切看起来都正常,但在真实站点上却不是。
JS 代码是最先执行的代码之一 - 在第二个 JS 文件的开头(第一个是 JQuery 插件) - 但它不起作用。
如有任何建议,我们将不胜感激。
最佳答案
我找到了解决方案 :) ...将 JS 代码更改为这个:
HTMLInputElement.prototype.focusCpy = HTMLInputElement.prototype.focus;
HTMLInputElement.prototype.focus = function () {
if( this.style.display != 'none' && this.style.visibility != 'hidden' && !this.disabled && this.type != 'hidden' && this.style.width != '0px' && this.style.width != '0%' && this.style.width != 0 ) {
HTMLInputElement.prototype.focusCpy.apply(this, arguments);
}
}
或者换句话说 - 我添加了对元素宽度的检查,因为一段时间后我想起了这个荒谬的浏览器 - IE - 经常渲染一些宽度和高度为 0px 的元素。
现在可以了:)
关于javascript - 使用 ASPX 和 DEVExpress 覆盖 IE 8 上的 JS focus() 方法 ...,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9023247/