如何使用 jQuery Migrate 插件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21882731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:44:47  来源:igfitidea点击:

How to use jQuery Migrate plugin

jqueryjquery-migrate

提问by Skye MacMaster

I'm using jquery 2.0 but would like to also use the jQuery migrate plugin so my website will work on older browsers. However, I've been unsuccessful at getting it to work. I have the following in the header section in my html.

我正在使用 jquery 2.0,但也想使用 jQuery migrate 插件,这样我的网站就可以在旧浏览器上运行。但是,我一直没有成功让它发挥作用。我的 html 的标题部分中有以下内容。

<head>
    <meta http-equiv="X-UA-Compatible" content="IE=8" />
    <script src="/Scripts/jquery-2.0.3.js"></script>
    <script src="/Scripts/jquery.unobtrusive-ajax.min.js"></script>
    <script src="/Scripts/jquery.validate.min.js"></script>
    <script src="/Scripts/jquery.validate.unobtrusive.min.js"></script>
    <script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
</head>

The compatibility meta tag is so I can test this on my computer (which has IE 11). I don't have a computer with an older IE. Anyway, this is giving me javascript errors such as:

兼容性元标记使我可以在我的计算机(具有 IE 11)上进行测试。我没有装有旧 IE 的计算机。无论如何,这给了我 javascript 错误,例如:

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

0x800a01b6 - JavaScript 运行时错误:对象不支持属性或方法“addEventListener”

The jQuery migrate guide (https://github.com/jquery/jquery-migrate/) seems to just say to include the migrate plugin after including jQuery. What am I doing wrong?

jQuery 迁移指南 ( https://github.com/jquery/jquery-migrate/) 似乎只是说在包含 jQuery 之后包含迁移插件。我究竟做错了什么?

EDIT

编辑

I found my local jquery.js file must be corrupt or maybe the nuget package I got it from has a bad version of it. Since that error goes away when I include jquery directly from code.jquery.com.

我发现我的本地 jquery.js 文件必须已损坏,或者我从中获取的 nuget 包的版本很差。因为当我直接从 code.jquery.com 包含 jquery 时,该错误就会消失。

<head>
    <meta http-equiv="X-UA-Compatible" content="IE=8" />
    <script src="http://code.jquery.com/jquery-2.1.0.js"></script>
    <script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
</head>

However, my scripts give an error. Here's an example script

但是,我的脚本出现错误。这是一个示例脚本

function HighlightSelectedRow(tr) {
    $("#TableSummary tr").removeClass("HighlightedRow");
    tr.className += " HighlightedRow";
}

It gives the following error

它给出了以下错误

0x800a138f - JavaScript runtime error: The value of the property '$' is null or undefined, not a Function object

Thanks

谢谢

回答by Jai

May be you should reorder the js stack:

可能你应该重新排序 js 堆栈:

<head>
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<script src="/Scripts/jquery-2.0.3.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script src="/Scripts/jquery.validate.min.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js"></script>
</head>

回答by Nikhil

Change the order of the include jquery migrate script

更改包含 jquery 迁移脚本的顺序

回答by Greg Little

Not likely it was a file corruption, just an incompatibility with jQuery 2.x and IE 8.If you need IE 8 compatibility, use the 1.x series.

不太可能是文件损坏,只是与 jQuery 2.x 和 IE 8 不兼容。如果您需要 IE 8 兼容性,请使用 1.x 系列。

This applies even if you are running IE 10 (and I would assume newer). In my case, I had a few PCs with Tools / Compatibility view settings / Display Intranet sites in compatibility view checked. So, it worked fine in Visual Studio on my local PC, but when I "published" to an in house server for the next layer of testing, SPLAT. Some PCs crash and burned, while others were fine.

即使您正在运行 IE 10(我假设更新),这也适用。就我而言,我有几台 PC,其中工具/兼容性视图设置/在兼容性视图中显示 Intranet 站点已选中。因此,它在我本地 PC 上的 Visual Studio 中运行良好,但是当我“发布”到内部服务器进行下一层测试时,SPLAT。一些 PC 崩溃并烧毁,而另一些则很好。

The jQuery download sitegives this warning.

jQuery 下载站点给出了这个警告。

jQuery 2.x has the same API as jQuery 1.x, but does not support Internet Explorer 6, 7, or 8. ... Since IE 6/7/8 are still relatively common, we recommend using the 1.x version unless you are certain no IE 6/7/8 users are visiting the site.

jQuery 2.x 的 API 与 jQuery 1.x 相同,但不支持 Internet Explorer 6、7 或 8。...由于 IE 6/7/8 仍然比较常见,我们建议使用 1.x 版本除非您确定没有 IE 6/7/8 用户正在访问该站点。

For those that want to force IE out of compatibility mode,

对于那些想要强制 IE 退出兼容模式的人,

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

can be added at the very top of your master page.

可以添加在母版页的最顶部。



Edit

编辑

This code will call the correct version of jQuery for older IEs. It will load 1.x for IE 8 and for 10 in compatibility mode. This can be added in combination with or as a replacement to the Meta value above.

此代码将为旧 IE 调用正确版本的 jQuery。它将在兼容模式下为 IE 8 和 10 加载 1.x。这可以与上面的 Meta 值结合添加或替换。

<!--[if lt IE 9]>
    <script src="http://code.jquery.com/jquery-1.11.0.js"></script>
<![endif]-->
<!--[if gte IE 9]><!-->
    <script src="http://code.jquery.com/jquery-2.1.0.js"></script>
<!--<![endif]-->

回答by Dhruv Rangunwala

Turning off compatibility mode on IE worked for me.

在 IE 上关闭兼容模式对我有用。