How to detect IE7 with jQuery?

2019-01-21 10:24发布

How to detect IE7 with jQuery possibly using jQuery.browser?

3条回答
劳资没心,怎么记你
2楼-- · 2019-01-21 11:07

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.

查看更多
做个烂人
3楼-- · 2019-01-21 11:13

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 */
    
查看更多
走好不送
4楼-- · 2019-01-21 11:16

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

查看更多
登录 后发表回答