【问题标题】:Trying to understand why meta compatibility tag resolved jQuery error试图理解为什么元兼容性标签解决了 jQuery 错误
【发布时间】:2014-03-18 18:17:01
【问题描述】:

我正在寻找有关特定修复为何起作用的解释,以便我能更好地了解正在发生的事情的机制。

我有一个 ASP.NET MVC 5 应用程序; “一切正常”(TM),直到我允许 NuGet 更新 jQuery 包。

然后,当我尝试在 IE 10 中运行 webapp 时,我开始收到以下错误消息/异常:

Unhandled exception at line 3398, column 4 in http://localhost:53242/Scripts/jquery-2.1.0.js

0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'addEventListener'

Unhandled exception at line 7, column 38 in http://localhost:53242/Scripts/bootstrap.js

0x800a139e - JavaScript runtime error: Bootstrap requires jQuery

我无法使用 Firefox 和 Chrome 重新创建。当我在这些浏览器中打开页面时,Firefox 27 和 Chrome 31 都没有抛出这些错误。

这是jquery-2.1.0.js的相关部分

jQuery.ready.promise = function( obj ) {
    if ( !readyList ) {
        
        readyList = jQuery.Deferred();
            
        // Catch cases where $(document).ready() is called after the browser event has already occurred.
        // we once tried to use readyState "interactive" here, but it caused issues like the one
        // discovered by ChrisS here: http://bugs.jquery.com/ticket/12282#comment:15
        if ( document.readyState === "complete" ) {
            // Handle it asynchronously to allow scripts the opportunity to delay ready
            setTimeout( jQuery.ready );
            
        } else {
            
            // Use the handy event callback
            document.addEventListener( "DOMContentLoaded", completed, false );  //This is line 3398
            
            // A fallback to window.onload, that will always work
            window.addEventListener( "load", completed, false );
        }
    }
    return readyList.promise( obj );
};

我发现的解决方法是添加这一行

<meta http-equiv="X-UA-Compatible" content="IE=edge;chrome=1" />

到我的_LayoutBootstrap.cshtml 文件的&lt;head&gt;

我的问题:

为什么将 meta 标记添加到文档的 head 可以解决我看到的错误?

给出的解释是标签“强制 IE 使用它可以使用的最佳模式”。假设这是正确的,那是什么意思?为什么 IE 无法在没有该标记的情况下找出正确的行为?


full detail here 中描述的问题以及在不受支持的 IE 8 中遇到相同问题的其他人。如果您好奇,请在帖子的大约 2/3 处包含重新创建步骤。

【问题讨论】:

    标签: jquery html asp.net


    【解决方案1】:

    它解决了您的问题,因为通过添加兼容性标志,您强制浏览器进入最新版本,而不是允许它选择兼容模式(即 IE7 模式,jquery 2.x 不支持)

    IE 默认为兼容模式,具体取决于您在浏览器中设置的设置。例如,默认情况下,内网站点(本地网络站点)会自动以兼容模式打开。

    当使用旧版本的 IE (IE

    【讨论】:

    • 这让我难以置信,我实际上必须告诉 IE “继续,告诉我你能做什么。”但我已经完成了足够多的企业 sysAd 工作来理解为什么会这样。感谢关于使用哪个版本的 jQuery 的条件逻辑的建议。
    猜你喜欢
    • 2019-03-20
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    相关资源
    最近更新 更多