我正在寻找有关特定修复为何有效的解释,以便我可以更好地理解正在发生的事情的机制。
我有一个 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/