jquery - 试图理解为什么元兼容性标记解决了 jQuery 错误

标签 jquery html asp.net

我正在寻找有关特定修复为何有效的解释,以便我可以更好地理解正在发生的事情的机制。

我有一个 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" />

<head>我的_LayoutBootstrap.cshtml文件。

我的问题:

为什么添加 meta标记到 head文档的一部分解决了我看到的错误?

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


full detail here 中描述的问题与另一个在不受支持的 IE 8 中遇到相同问题的人一起。如果您好奇,重新创建步骤包含在大约 2/3 的帖子中。

最佳答案

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

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

当使用旧版本的 IE (IE<10) 时,您应该使用条件注释来包含 1.1x 版本的 jquery,这将完全避免此问题,允许您的用户使用他们想要的任何浏览器而无需修改他们的设置以正确使用您的应用程序。

关于jquery - 试图理解为什么元兼容性标记解决了 jQuery 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21863179/

相关文章:

jquery - 输入文本 在占位符中添加其他颜色的星号

html - 网站上的颜色与设计软件上显示的颜色不同

html - 电子邮件背景颜色上的背景图像

c# - asp.net无法获取输入文本的文本

asp.net - asp.net中Literal控件的Mode属性

javascript - 每当扩展子 div 时如何扩展容器 div 的高度

jQuery验证插件——错误高亮问题

jquery - Bootstrap 3 Effects div 滚动

c# - 在 Repeater 中传递行的 ID 以提交 ASP NET 中的按钮

Javascript 数组不会更新