从 jQuery 1.x 升级到 jQuery 2.x

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23139453/
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 02:11:46  来源:igfitidea点击:

Upgrade from jQuery 1.x to jQuery 2.x

jqueryupgrade

提问by ucefkh

I am trying to upgrade from jQuery 1.x to jQuery 2.x.

我正在尝试从 jQuery 1.x 升级到 jQuery 2.x。

I have jQuery 1.8 and jQueryUI 1.8, and now I want to upgrade to jQuery 2.x and enhance my web app.

我有 jQuery 1.8 和 jQueryUI 1.8,现在我想升级到 jQuery 2.x 并增强我的 Web 应用程序。

So my question is what to change and remove, like for Ajaxand events.

所以我的问题是改变和删除什么,比如Ajaxevents

Like these errors I am talking about

就像我说的这些错误

Uncaught TypeError: Object [object Object] has no method 'live'

Did functions like .on() and .ajax() change any? (I know .live was deprecated).

像 .on() 和 .ajax() 这样的函数有什么改变吗?(我知道 .live 已被弃用)。

P.S. I have to add Zurb Foundation 5 which uses jQuery 2.

PS 我必须添加使用 jQuery 2 的 Zurb Foundation 5。

采纳答案by ucefkh

If you are thinking of upgrading to jQuery 1.x to jQuery 2.x should consider this and take these steps :) :

如果您正在考虑将 jQuery 1.x 升级到 jQuery 2.x,请考虑这一点并采取以下步骤:):

1.No More Support for IE6/7/8

1.不再支持 IE6/7/8

I still think it's a little premature to abandon IE8 but the team couldn't wait any longer. jQuery 2.0 removes all the legacy IE code for node selection, DOM manipulation, event handling and Ajax.

我仍然认为放弃 IE8 为时过早,但团队不能再等了。jQuery 2.0 删除了所有用于节点选择、DOM 操作、事件处理和 Ajax 的遗留 IE 代码。

2.Custom build feature has been refined in version 2.0

2. 2.0版本完善了自定义构建功能

so you can exclude any of 12 unused modules and shrink jQuery below 10Kb. The modules which can be omitted are: List of Functions

所以你可以排除 12 个未使用的模块中的任何一个,并将 jQuery 压缩到 10Kb 以下。可以省略的模块有:函数列表

Should I Upgrade?

我应该升级吗?

It's important to understand that jQuery 2.0 has API parity with jQuery 1.9. There are a small number of bug fixes but no new features.

重要的是要了解 jQuery 2.0 与 jQuery 1.9 具有相同的 API。有少量错误修复,但没有新功能。

However, if you're one of those lucky developers who has dropped support for IE6/7/8, grab jQuery 2.0 and don't look back.

但是,如果您是已放弃对 IE6/7/8 支持的幸运开发人员之一,请抓住 jQuery 2.0 并且不要回头。

回答by Victor

  1. Do not use offsetoption in position properties, e.g. code $element.position({my: 'center center', at: 'center center', offset: '5 -10'})should be replaced with $element.position({my: 'center center', at: 'center+5 center-10'}).
  2. Do not use $element.bind(), $element.live(), $element.delegate()to assign event handler, use $element.on().
  3. Do not use browser sniffing with $.browser, try to use feature detection instead ($.support).
  4. Do not use deferred.isRejected(), deferred.isResolved(), use deferred.state()instead. Do not use deferred.pipe(), the deferred.then()method should be used instead.
  5. Do not use the $elements.size()method, use the $elements.lengthproperty instead. The .size()method is functionally equivalent to the .lengthproperty; however, the .lengthproperty is preferred because it does not have the overhead of a function call.
  6. Checkbox/radio state in a .trigger()ed "click" event now has the same state as in a user-initiated action.
  7. Changed naming convention for .data()keys, e.g., ui-dialoginstead of dialog. (http://jqueryui.com/upgrade-guide/1.9/#changed-naming-convention-for-data-keys).
  8. Do not use $.ui.contains(), use $.contains()instead.
  9. Each widget instance already has unique identifier this.uuidand event namespace this.eventNamespace = "." + this.widgetName + this.uuid. Do not generate similar things manually.
  10. Do not use $element.focus(n)- it is deprecated. Use setTimeout(function() { $element.focus(); }, n);.
  11. Do not use $element.zIndex()- it is deprecated.
  12. Do not use $.ui.keyCode.NUMPAD_*constants - they are removed.
  13. Do not use $element.data('someWidget')to retrieve widget instance. Use instance()method: $element.someWidget('instance'). Unlike other plugin methods, the instance()method is safe to call on any element. If the element is not an instance of the given widget, the method returns undefined: $('<div></div>').dialog('instance') /* returns undefined instead of throwing Error */.
  1. 不要offset在位置属性中使用选项,例如代码$element.position({my: 'center center', at: 'center center', offset: '5 -10'})应替换为$element.position({my: 'center center', at: 'center+5 center-10'})
  2. 不要使用$element.bind()$element.live()$element.delegate()来分配事件处理程序,使用$element.on()
  3. 不要使用浏览器嗅探与$.browser,尝试使用特征检测代替 ( $.support)。
  4. 不要使用deferred.isRejected(), deferred.isResolved(),deferred.state()而是使用。不要使用deferred.pipe()deferred.then()应该使用该方法。
  5. 不要使用$elements.size()方法,$elements.length而是使用属性。该.size()方法在功能上等同于该.length属性;但是,该.length属性是首选的,因为它没有函数调用的开销。
  6. .trigger()ed“单击”事件中的复选框/单选状态现在与用户启动的操作具有相同的状态。
  7. 更改了.data()键的命名约定,例如,ui-dialog而不是dialog. (http://jqueryui.com/upgrade-guide/1.9/#changed-naming-convention-for-data-keys)。
  8. 不要使用$.ui.contains()$.contains()而是使用。
  9. 每个小部件实例已经具有唯一标识符this.uuid和事件命名空间this.eventNamespace = "." + this.widgetName + this.uuid。不要手动生成类似的东西。
  10. 不要使用$element.focus(n)- 它已被弃用。使用setTimeout(function() { $element.focus(); }, n);.
  11. 不要使用$element.zIndex()- 它已被弃用。
  12. 不要使用$.ui.keyCode.NUMPAD_*常量 - 它们被删除。
  13. 不要$element.data('someWidget')用于检索小部件实例。使用instance()方法:$element.someWidget('instance')。与其他插件方法不同,该instance()方法可以安全地调用任何元素。如果元素不是给定小部件的实例,则该方法返回undefined: $('<div></div>').dialog('instance') /* returns undefined instead of throwing Error */

Original upgrade guides and full list of changes:

原始升级指南和完整更改列表:

回答by Kevin B

In jQuery 1.9, several methods were removed that were available in prior versions of jquery.

在 jQuery 1.9 中,移除了一些在先前版本的 jquery 中可用的方法。

If you are using those methods, then yes, you will run into problems.

如果您正在使用这些方法,那么是的,您会遇到问题。

Otherwise, no you will not run into problems.

否则,不,您不会遇到问题。

Using the jQuery migrate plugin that you mentioned will solve all of the problems that you could possibly have with upgrading from 1.8 to 1.9+(which includes 2.x) and it will also inform you of any methods you are using that have been removed when you look at the console. The migrate plugin is the best way of upgrading jquery from 1.6x-1.8x to 1.9+/2.0+. Include jquery 2.x, then include the migrate plugin, then open your console and replace old methods until the migrate plugin stops giving your warnings. At that point you should be able to safely remove the migrate plugin.

使用您提到的 jQuery migrate 插件将解决从 1.8 升级到 1.9+(包括 2.x)时可能遇到的所有问题,并且它还会通知您正在使用的任何方法已被删除你看看控制台。迁移插件是将 jquery 从 1.6x-1.8x 升级到 1.9+/2.0+ 的最佳方式。包含 jquery 2.x,然后包含 migrate 插件,然后打开控制台并替换旧方法,直到 migrate 插件停止发出警告。那时您应该能够安全地删除迁移插件。