jQuery 1.9较之前版本的变化,主要介绍移除方法的替代方法:.browser、.live、.die、.sub、.toggle

简介:

本文档是一次调研总结,直接贴过来的,没有作背景铺垫修改。这是一个关于jQuery1.9较之前版本变化的调研,完了之后,总结了这么一个对于“jQuery 1.9移除方法的替代总结”的文档,不一定是最优的解决方案,但我所提到的方法绝对可行。

 

本文档总结了一些jQuery 1.9较之前版本的变化(原文:http://jquery.com/upgrade-guide/1.9/),看官方Blog,JQuery2.0已经出到了Beta2 Released版本,并且不再支持IE 6/7/8.

以下为涉及到Gaia 1.0的几个变化:

 

jQuery.browser()

jQuery.browser() removed

The jQuery.browser() method has been deprecated since jQuery 1.3 and is removed in 1.9. If needed, it is available as part of the jQuery Migrate plugin. We recommend using feature detection with a library such as Modernizr.

这里给出的是用Modernizr(一个利用JS和CSS来检测浏览器所支持功能的小工具)来检测浏览器所支持功能,其实官网还给出了另一种解决方案:

Because $.browser uses navigator.userAgent to determine the platform, it is vulnerable to spoofing by the user or misrepresentation by the browser itself. It is always best to avoid browser-specific code entirely where possible. The$.support property is available for detection of support for particular features rather than relying on $.browser.

Gaia1.0中用到JQuery.browser()方法的为:

$.browser.msie && $.browser.version <= 8

作为常用的两种方法,

JQuery.browser.mise(如果是IE则返回true)可以用JQuery.support.boxModel(如果IE浏览器是QuirksMode方式运行,则返回false)代替;

jQuery.browser.version <= 8可以用jQuery. support.leadingWhitespace(判断浏览器是否为IE 6~8版本)代替;

这样上述语句可以改为:

$.support.boxModel && $.support.leadingWhitespace

另外,jQuery.support.objectAll可判断浏览器是否为IE 7~8版本。由于jQuer2.0不再支持IE9之前的版本,日后升级还需根据官方推荐判断浏览器类型及版本加载不同的jQuery。如官方推荐方式;

<!--[if lt IE 9]>

<script src='jquery-1.9.0.js'></script>

<![endif]-->

<!--[if gte IE 9]>

<script src='jquery-2.0.0.js'></script>

<![endif]-->

如果必须要继续使用jQuery.browser()可以添加“jquery-browser”插件,但我没有测试该插件。

 

.live()

link .live() removed

The .live() method has been deprecated since jQuery 1.7 and has been removed in 1.9. We recommend upgrading code to use the .on() method instead. To exactly match $("a.foo").live("click", fn), for example, you can write $(document).on("click", "a.foo", fn). For more information, see the .on() documentation. In the meantime, the jQuery Migrate plugin can be used to restore the .live() functionality.

.live()方法在1.9中移除,@ZPS在邮件中已经告知过大家。对于.live()方法的移除,升级比较简单,仅仅是将“.live()”替换为“.on()”。

 

.die()

.die() removed

The .die() method has been deprecated since jQuery 1.7 and has been removed in 1.9. We recommend upgrading code to use the .off() method instead. To exactly match $("a.foo").die("click"), for example, you can write $(document).off("click", "a.foo"). For more information, see the .off() documentation. In the meantime, the jQuery Migrate plugin can be used to restore the .die() functionality.

相对于“.live()”方法的移除,“.die()”方法也从1.9中移除,取而代之的是“.off()”方法。正如在1.9之前,很多人只关注过“.live()”方法,却不知道还有个“.die()”方法,或许还会有Coder不知道如何去掉.on()添加的事件,其实就是用“.off()”进行删除添加的事件。

 

jQuery.sub()

jQuery.sub() removed

The jQuery.sub() method has been moved to the jQuery Migrate plugin. The number of use cases where it proved valuable were not enough to justify keeping it in core. The jQuery Migrate plugin adds back this functionality.

.sub()方法可以创建一个新的jQuery副本而不影响原有的jQuery对象,我对该方法的理解是:其实.sub()方法就是增加或重写jQuery的方法或创建新plugin,有待讨论。

从上面升级指南上来看,.sub()方法并没有被removed,而是被moved到其他plugin,所以应该是还可以用的,只要引用相应的plugin。

官方给出的使用.sub()的两个特定情况:一是在不改变原有方法的前提下提供一种简单的重写jQuery方法的途径,二是帮助用户解决jQuery plugin封装和基本命名空间。翻译晦涩,大家请看原文:

There are two specific use cases for which jQuery.sub() was created. The first was for providing a painless way of overriding jQuery methods without completely destroying the original methods and another was for helping to do encapsulation and basic namespacing for jQuery plugins.

 

.toggle(function, function, … )

link .toggle(function, function, ... ) removed

This is the "click an element to run the specified functions" signature of .toggle(). It should not be confused with the "change the visibility of an element" of .toggle() which is not deprecated. The former is being removed to reduce confusion and improve the potential for modularity in the library. The jQuery Migrate plugin can be used to restore the functionality.

需要注意的是该.toggle()是“绑定两个或多个处理程序,在点击时循环执行”;另一个.toggle()仍然存在,它是“控制相应组件的显示和隐藏”;中文晦涩,官方对此二方法的说明如下:

Categories: Deprecated > Deprecated 1.8 | Events > Mouse Events

.toggle(handler(eventObject), handler(eventObject) [,handler(eventObject)])

Returns:jQuery

version deprecated: 1.8, removed: 1.9

Description:Bind two or more handlers to the matched elements, to be executed on alternate clicks.

Categories: Effects > Basics

.toggle( [duration ] [, complete ] )

Returns:jQuery

Description:Display or hide the matched elements.

这个变化值得注意。对于删除的这个“.toggle()”方法,官方没有给出升级措施,但我发现一个方法名和描述都比较相似的方法“.trigger()”,不知道可不可以替代,还请大家赐教。

 

另外,国外有论坛提到“jQuery Migrate Plugin”插件,可以使用该插件检测在jQuery 1.9 或2.0中哪些功能已经启用或移除。我还没有学习到,大家参看项目README吧。(谷歌这个插件,全是E文;百度这个插件,过半是E文)

 

页面凌乱,希望能帮到大家,谢谢

Lionden 2013年3月20日

E-Mail:hsdlionden@gmail.com

转载请注明博客园:http://www.cnblogs.com/lionden/

posted on 2013-03-21 14:44  Lionden  阅读(23380)  评论(9编辑  收藏  举报

导航

 联系我 | 新浪微博关注我 | 返回首页 
版权所有 © Lionden Lee · Beijing
电子邮箱:hsdlionden@hotmail.com