【问题标题】:Detect IE9 without feature detection检测IE9而不进行特征检测
【发布时间】:2013-07-22 18:38:06
【问题描述】:

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

我已经解决了我的问题(对于那些感兴趣的人,我问了一个关于这个问题的问题 here 但实际上,这无关紧要)。

现在我只想为 IE9 实施这个 hack 修复,因为这是让我头疼的原因。

那么检测 IE 9 的最佳方法是什么?

【问题讨论】:

标签: jquery internet-explorer-9 browser-detection


【解决方案1】:

这些 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)
 }

【讨论】:

【解决方案2】:

使用 JQuery(1.8 或更低版本)

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

【讨论】:

  • browser 属性在很久以前就被弃用了,并在 jQuery 1.9 中被删除。
  • 没有意识到这有一个版本以及一个 msie 标志!
  • @JamesAllardice,我没想到,他们为什么要那样做!
  • 正如@JamesAllardice 所说,我也很警惕将自己与旧版本的 Jquery 捆绑在一起!
  • @AshBurlaczenko,因为您确实应该进行功能检测而不是浏览器版本嗅探
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-27
  • 2015-08-12
  • 2016-12-19
  • 2010-11-20
  • 1970-01-01
相关资源
最近更新 更多