jQuery 1.9 移除了 $.browser 的替代方法

January 16th, 2013FwolfLeave a commentGo to comments

授权方式:署名,非商业用途,保持一致,转载时请务必以超链接(http://www.fwolf.com/blog/post/35)的形式标明文章原始出处和作者信息及本声明。

jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version , 取而代之的是 $.support 。 在更新的 2.0 版本中,将不再支持 IE 6/7/8。 以后,如果用户需要支持 IE 6/7/8,只能使用 jQuery 1.9。 如果要全面支持 IE,并混合使用 jQuery 1.9 和 2.0, 官方的解决方案是:

<!--[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]-->

从长久来看,这样有利于在复杂情况下根据浏览器特性进行分别处理, 而不是简单的检测浏览器类型和版本。 但目前很多旧程序的移植恐怕无法直接过渡为根据浏览器支持特性, 所以在网上找了一些能够直接替换的解决办法。

判断浏览器类型:

$.browser.mozilla = /firefox/.test(navigator.userAgent.toLowerCase());
$.browser.webkit = /webkit/.test(navigator.userAgent.toLowerCase());
$.browser.opera = /opera/.test(navigator.userAgent.toLowerCase());
$.browser.msie = /msie/.test(navigator.userAgent.toLowerCase());

等号后面的表达式返回的就是 true/false, 可以直接用来替换原来的 $.browser.msie 等。

检查是否为 IE6:

// Old
if ($.browser.msie && 7 > $.browser.version) {}
// New
if ('undefined' == typeof(document.body.style.maxHeight)) {}

检查是否为 IE 6-8:

if (!$.support.leadingWhitespace) {}

终极方法是用另外的类库替代,比如 这个 , 但作者也不推荐使用浏览器类型和版本来进行判断。

参考

随机推荐

  1. leetcode 233 Number of Digit One

    这题属于需要找规律的题.先想一下最简单的情形:N = 10^n - 1 记X[i]表示从1到10^i - 1中 1 的个数,则有如下递推公式:X[i] = 10 * X[i - 1] + 10^(i ...

  2. 搭建linux系统环境

    1.安装centos6.4 x86,选择的是Desktop模式2.配置centos a关闭NetwrokManager(chkconfig NetworkManager off) b开机启动netwo ...

  3. ajax注释

    //xmlHttpRequest,但是这个对象只是在火狐,google...//在中国用的最广泛的IE浏览器里面是没有这个对象的//在IE里面是用的一个控件来解决这个问题,ActiveXObject/ ...

  4. phpstorm-file watcher

    在项目中使用了sass,将scss编译成css的时候,每次都需要compass watch netbeans产品带有file watcher功能 三大类 1,less,scss,sass into c ...

  5. Unity3D入门(一):环境搭建

    1.Unity3D 目前最新正式版本是4.2.1f  官网下载,以前的版本安装时候需要序列号激活,新版本4.2.1f 不需要,完全免费,但发布的时候需要许可证 2.要学习的同学,下载频道可以找到破解补 ...

  6. Inno Setup 打包工具总结

    Inno Setup 打包工具总结 分类: Install Setup 2013-02-02 15:44 2386人阅读 评论(0) 收藏 举报 最近打包用到了Inno setup,在这个过程中容易犯 ...

  7. 用C语言计算圆的面积~!!!!!!!

    #include <stdio.h>void main(){ int a,b,c,y,g,f; printf("圆柱底面的半径,圆柱的高"); scanf(" ...

  8. 4 Values whose Sum is 0

      Time Limit:15000MS     Memory Limit:228000KB     64bit IO Format:%I64d & %I64u Submit Status D ...

  9. apache2将http自动指向https

    <VirtualHost *:80> RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_H ...

  10. 兼容性所有浏览器的透明CSS设置

    兼容所有浏览器的透明CSS设置: .transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; -khtml-opacity: 0. ...