jquery1.9变化

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 asModernizr.

这里给出的是用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()”。

 on方法可以接受三个参数:事件名、触发选择器、事件函数。中间有个触发选择器的参数,使用它就可以实现live的效果。这个触发选择器实际上就是在JQ内部判断了一次事件参数的$(e.target).is(selector),只有触发对象匹配触发选择器才会触发。这是利用了事件冒泡的机制来完成的,原本的live也是使用冒泡机制所以既然on可以实现那么live也就没有存在的必要了,只不过为了兼容让它从1.7苟延残喘的活到了1.9而已。
  这篇文章也没啥内容了,接下来就用这个功能做点有意义的事情示范下吧~ 在低版本IE中A标签在鼠标按下时候会出现虚线边框,这是由focus造成的。我们只要在全局事件中做点手脚就能解决这个问题。在现代浏览器中focus是不冒泡的,但是低版本浏览器中可以冒泡。所以对于低版本浏览器中对focus使用live是有效的。在jQuery1.9之前的版本我们可以这样写:$("a").live("focus",function(){
  this.blur();
});
  jQuery1.9之后由于live被删除了,所以应该这样写$(document).on("focus","a",function(){
  this.blur();
});
  还要注意个问题,如果是从live的写法换成on的写法别忘了调整调用链。因为live的返回值是事件触发的对象,而使用on则是在容器对象上。//jQuery1.9-
$("#panel").find("div").live("click",function(){
  alert("x");
}).addClass("x");

//jQuery1.9+
$("#panel").on("click","div",function(){
  alert("x");
}).find("div").addClass("x");


.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()”,不知道可不可以替代,还请大家赐教。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值