How to detect IE7 with jQuery?

半腔热情 提交于 2019-11-28 16:58:30

Got a method

if ($.browser.msie  && parseInt($.browser.version, 10) === 7) {
  alert('IE7'); 
} else {
  alert('Non IE7');
}

-- update

Please note that $.browser is removed from jQuery 1.9

See $.browser. This feature has been deprecated and you should consider $.support instead.

To answer the question, this is a rock solid technique primarily for use in stylesheets but can also be used easily to check browser version, or best still part of a selector.

  1. Use the following markup (from HTML5 Boilerplate)

    <!--[if lt IE 7]><html class="ie6"><![endif]-->
    <!--[if IE 7]><html class="ie7"><![endif]-->
    <!--[if IE 8]><html class="ie8"><![endif]-->
    <!--[if gt IE 8]><!--><html><!--<![endif]-->
    <head>
    
  2. Use a selector with hasClass in jQuery

    $('html').hasClass('ie7');
    $('html.ie7 .myclass').dostuff();
    
  3. And use as part of a normal selector in css

    .mydiv {max-height:50px}
    .ie6 .mydiv {height:50px} /* ie6 max-height fix */
    
Nils

All other things considered:

if ($.browser.msie && $.browser.version == 7) {

    //do something

    };

Should work. Whether or not it is the right way to go about things is another question.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!