如果浏览器是ie9,则执行某些javascript

mel*_*dit 6 javascript jquery internet-explorer internet-explorer-9

如果浏览器低于IE9,我想在jQuery中执行某个代码.是的,我已经知道了

<!--[if ... IE ]> <![endif]-->
Run Code Online (Sandbox Code Playgroud)

但我想要的是在scipt标签和jQuery document.ready中检查这个条件

<script>
    $(document).ready(function(){
        //code to check if lt ie9 
    });
</script>
Run Code Online (Sandbox Code Playgroud)

Dan*_*iel 14

您可以通过<html>元素上的类来定位较旧的Internet Explorer版本(<= 9)...

<!--[if lt IE 7]>  <html class="ie ie6 lte9 lte8 lte7"> <![endif]-->
<!--[if IE 7]>     <html class="ie ie7 lte9 lte8 lte7"> <![endif]-->
<!--[if IE 8]>     <html class="ie ie8 lte9 lte8"> <![endif]-->
<!--[if IE 9]>     <html class="ie ie9 lte9"> <![endif]-->
<!--[if gt IE 9]>  <html> <![endif]-->
<!--[if !IE]><!--> <html>             <!--<![endif]-->
Run Code Online (Sandbox Code Playgroud)

...然后检查是否<html>有类.lt9.lt8- 您要定位的Internet Explorer的任何版本:

if($('html').hasClass('lte9')) { /* LTE IE9 */ }
Run Code Online (Sandbox Code Playgroud)

但是,我建议使用Modernizr功能检测,而不是检查特定的浏览器.