sal*_*ane 102 javascript jquery
我不仅要检测浏览器类型,还需要使用jQuery检测版本.大多数情况下,我需要找出它是否是IE 8.
我不确定我是否正确地做到了.
如果我做 :
if (jQuery.browser.version >= 8.0) {
dosomething}
Run Code Online (Sandbox Code Playgroud)
我不确定它适用于版本8.123.45.6还是会?
编辑:请注意,JQuery 2+已经放弃了对IE8及更低版本的支持,因此无法再用于检测IE8.如果使用当前版本的JQuery,则必须使用非JQuery解决方案.
meo*_*meo 173
我认为最好的方法是:
来自HTML5样板:
<!--[if lt IE 7]> <html lang="en-us" class="no-js ie6 oldie"> <![endif]-->
<!--[if IE 7]> <html lang="en-us" class="no-js ie7 oldie"> <![endif]-->
<!--[if IE 8]> <html lang="en-us" class="no-js ie8 oldie"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en-us" class="no-js"> <!--<![endif]-->
Run Code Online (Sandbox Code Playgroud)
在JS中:
if( $("html").hasClass("ie8") ) { /* do your things */ };
Run Code Online (Sandbox Code Playgroud)
特别是因为$.browser已从jQuery 1.9+中删除.
Mit*_*ran 89
这适用于所有IE8次要版本
if ($.browser.msie && parseInt($.browser.version, 10) === 8) {
alert('IE8');
} else {
alert('Non IE8');
}
Run Code Online (Sandbox Code Playgroud)
- 更新
And*_*Dog 79
它在jQuery API文档中有记录.检查Internet Explorer,$.browser.msie然后检查其版本$.browser.version.
自jQuery 1.3以来,jQuery.browser()方法已被弃用,并在1.9中被删除.如果需要,它可以作为jQuery Migrate插件的一部分使用.我们建议对Modernizr等库使用特征检测.
The*_*Saw 56
不要忘记您也可以使用HTML来检测IE8.
<!--[if IE 8]>
<script type="text/javascript">
ie = 8;
</script>
<![endif]-->
Run Code Online (Sandbox Code Playgroud)
在你所有的脚本之前让你只需检查"ie"变量或其他什么.
ken*_*bec 27
如果浏览器不是IE8,则document.documentMode是未定义的,
标准模式返回8,"兼容IE7"返回7
如果它作为IE7运行,则会有许多不支持的css和dom功能.
use*_*ca8 15
假设...
<body>或<html>)...那么这可能是最好的技巧(基于这种非jQuery,稍微不那么灵活的变体).它创建然后测试然后删除适当的条件注释.
(在IE10 +'标准模式'中忽略了条件注释 - 但这应该没问题,因为IE10 +'标准模式'没有疯狂的渲染引擎!)
放入此功能:
function isIE( version, comparison ){
var $div = $('<div style="display:none;"/>');
// Don't chain these, in IE8 chaining stops some versions of jQuery writing the conditional comment properly
$div.appendTo($('body'));
$div.html('<!--[if '+(comparison||'')+' IE '+(version||'')+']><a> </a><![endif]-->');
var ieTest = $div.find('a').length;
$div.remove();
return ieTest;
}
Run Code Online (Sandbox Code Playgroud)
然后像这样使用它:
if(isIE()){ /* runs in all versions of IE after 4 before standards-mode 10 */ }
if(isIE(8)){ /* runs in IE8 */ }
if(isIE(9)){ /* runs in IE9 */ }
if(isIE(8,'lte')){ /* runs in IE8 or below */ }
if(isIE(6,'lte')){ /* if you need this, I pity you... */ }
Run Code Online (Sandbox Code Playgroud)
我还建议缓存此函数的结果,这样您就不必重复它.例如,您可以使用字符串(comparison||'')+' IE '+(version||'')作为键来存储和检查某个对象中的此测试的结果.
注意:
1)$ .browser似乎被丢弃在jQuery 1.9+中(如Mandeep Jain所述).这是推荐使用.支持来代替.
2)当浏览器处于"兼容性"模式时,$ .browser.version可以在IE> 7中返回"7".
3)从IE 10开始,条件注释将不再有效.
4)jQuery 2.0+将放弃对IE 6/7/8的支持
5)document.documentMode似乎仅在Internet Explorer 8+浏览器中定义.返回的值将告诉您Internet Explorer正在运行的"兼容性"模式.但仍然不是一个好的解决方案.
我尝试了很多.support()选项,但看起来当IE浏览器(9+)处于兼容模式时,它只会表现得像IE 7 ...... :(
到目前为止,我只发现这个工作(kind-a):
(如果没有定义documentMode并且不支持htmlSerialize和opacity,那么你很可能会看到IE <8 ...)
if(!document.documentMode && !$.support.htmlSerialize && !$.support.opacity)
{
// IE 6/7 code
}
Run Code Online (Sandbox Code Playgroud)
如果你摆弄浏览器版本,它往往不会带来任何好处.你不想自己实现它.但你可以使用Paul Irish和其他聪明人制作的Modernizr.它将检测浏览器实际可以执行的操作并将适当的类放入<html>元素中.但是使用Modernizr,您可以像这样测试IE版本:
$('html.lt-ie9').each() {
// this will execute if browser is IE 8 or less
}
Run Code Online (Sandbox Code Playgroud)
同样,你可以使用.lt-ie8,和.lt-ie7.
| 归档时间: |
|
| 查看次数: |
192987 次 |
| 最近记录: |