【问题标题】:IE8 jQuery error at line "elem[ type ]();"IE8 jQuery 错误在“elem [ type ]();”行
【发布时间】:2013-01-31 18:45:43
【问题描述】:

我在 IE8 中使用 jQuery 1.7.2、1.8.0 或 1.8.3 时遇到问题。该网页在 Chrome、Firefox、IE9、Safari 和 Opera 中运行良好。

在IE8下开发者工具提示如下错误:

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

此错误发生在 jQuery 1.8.0 和 1.8.3 中的第 2973 行(在 1.7.2 中为第 3242 行):elem[ type ](); 存在于 trigger 函数中。

我该如何解决这个问题?或者至少知道是哪个trigger 执行导致了这个问题。

任何提示将不胜感激。

【问题讨论】:

标签: javascript jquery internet-explorer-8


【解决方案1】:

这是 IE 中一个老生常谈的错误(很高兴知道它已在 8 中修复)。我不知道官方原因,但我认为这与 IE 在执行上下文完成后才重新绘制 DOM 有关,同时尝试focus() 认为它仍然隐藏的元素:

function calledAtSomePoint() { // begin execution

    // ...

    element.style.display = ''; // show container
    input.focus(); // IE thinks element is hidden 

    // end of execution, IE repaints the DOM but it's too late
} 

The solution is to use setTimeout:

setTimeout(function() {
    document.getElementById('add-comment-login-overlay-username-input').focus()
}, 0)

我曾经多次遇到过这种情况,包括使用 jQuery。这不是任何图书馆的错。 setTimeout 一直为我工作。

【讨论】:

    【解决方案2】:

    阅读这篇文章后http://bugs.jquery.com/ticket/10859(由@nez 在 cmets 中指出)。我在我的代码中搜索了.focus 调用,大约有 50 个调用分布在第三方(如 jQuery-ui、jquery.validate 和我的代码本身)之间。我决定更改 jQuery 1.8.3 本身,而不是更改一堆其他第三方库。

    所以我在 jQuery-1.8.3.js 中更改了以下第 2973 行:

    elem[ type ]();
    

    到:

    try {
      this.newelement[0].focus();
    } catch(err){}
    

    【讨论】:

    • 是的,由于 jQuery UI(我无法真正修改),我遇到了这个问题。我要试试你的解决方法。我想知道(并希望)新版本的 jQuery 是否修复了这个错误。更现实地说,我希望我的用户群能够从 IE8 升级(2015 年 9 月仍有 20% 的用户还在使用它,WTF?)。仅供参考,在我的代码中是第 2972​​ 行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    • 2016-11-25
    • 2021-09-04
    • 2010-10-21
    • 1970-01-01
    相关资源
    最近更新 更多