2013-07-23 99 views
2

所以我需要检测IE 9.我知道我应该真的使用feature detection,但我不知道什么功能导致我的问题我所知道的是,即9是导致我的问题。检测IE9没有功能检测

我有一个解决我的问题的方法(对于那些有兴趣的人,我问了一个关于问题here的问题,但真的,这是无关紧要的)。

现在我想实施这 黑客 修复只为IE9,因为这是什么让我头痛。

那么如何检测IE 9的最佳方式?

+0

IE条件评论... – Musa

+0

http://api.jquery.com/jQuery.browser/ – mavili

+1

$ .browser一直以来的jQuery 1.3弃用并已在1.9中删除 – jmoerdyk

回答

5

这些IE条件语句会给你一个CSS类钥匙关闭的:

<!--[if lt IE 7]> <html class="lt-ie10 lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]--> 
<!--[if IE 7]> <html class="lt-ie10 lt-ie9 lt-ie8" lang="en"> <![endif]--> 
<!--[if IE 8]> <html class="lt-ie10 lt-ie9" lang="en"> <![endif]--> 
<!--[if IE 9]> <html class="lt-ie10" lang="en"> <![endif]--> 
<!--[if gt IE 9]><!--> <html lang="en"> <!--<![endif]--> 

所以,如果你只关心IE9,你可以这样做:

<!--[if IE 9]> <html class="ie9" lang="en"> <![endif]--> 

或者,保持与约定:

<!--[if IE 9]> <html class="lt-ie10" lang="en"> <![endif]--> 

然后,你的JS(和CSS)可键关机的HTML.lt-IE10选择:

if ($('HTML.lt-ie10').length) { 
    //this is IE9 and older 
} 
else { 
    //this is not IE9 and older (so it could be Chrome, or Safari or IE10, etc) 
} 
+0

为什么这是低票?我会说条件是一个很好的解决方案... – pudelhund

+1

这里有更多关于如何使用它们的信息: http://www.positioniseverything.net/articles/cc-plus.html – pudelhund

+3

不妨让最后一个'因为IE10会像每隔一个非IE浏览器那样处理条件注释。因为IE10将会像其他非IE浏览器一样处理条件注释。 ;) –

1

使用jQuery(1.8或更低)

if ($.browser.msie && $.browser.version == 9) { 
    // Your code here 
} 
+3

'浏览器'属性已被弃用,并在jQuery 1.9中被删除。 –

+0

没有意识到这有一个版本以及msie国旗! – Liam

+0

@JamesAllardice,我没有意识到,他们为什么要那样做!作为@JamesAllardice的 –