How do I target only Internet Explorer 10 for certain situations like Internet Explorer-specific CSS or Internet Explorer-specific JavaScript code?

前端 未结 25 2618
谎友^
谎友^ 2020-11-22 06:19

How do I target only Internet Explorer 10 for certain situations like Internet Explorer-specific CSS or Internet Explorer-specific JavaScript code?

I tried this, but

相关标签:
25条回答
  • 2020-11-22 06:29

    You could use feature detection to see if browser is IE10 or greater like so:

    var isIE = false;
    if (window.navigator.msPointerEnabled) {
        isIE = true;
    }
    

    Only true if > IE9

    0 讨论(0)
  • 2020-11-22 06:31

    If you must do this, you can check the user agent string in JavaScript:

    var isIE10 = !!navigator.userAgent.match(/MSIE 10/);
    

    As other people have mentioned, I'd always recommend feature detection instead.

    0 讨论(0)
  • 2020-11-22 06:32

    Perhaps you can try some jQuery like this:

    if ($.browser.msie && $.browser.version === 10) {
      $("html").addClass("ie10");
    }
    

    To use this method you must include the jQuery Migrate library because this function was removed from the main jQuery library.

    Worked out quite fine for me. But surely no replacement for conditional comments!

    0 讨论(0)
  • 2020-11-22 06:32

    For me the following code works fine, all conditional comments are working in all IE versions:

    <!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
    <!--[if IE 7 ]>    <html class="ie7"> <![endif]-->
    <!--[if IE 8 ]>    <html class="ie8"> <![endif]-->
    <!--[if IE 9 ]>    <html class="ie9"> <![endif]-->
    <!--[if (gt IE 11)|!(IE)]><!--> <html> <!--<![endif]-->
    
    <script>
        if (document.documentMode===10){
            document.documentElement.className+=' ie10';
        }
        else if (document.documentMode===11){
            document.documentElement.className+=' ie11';
        }
    </script>
    

    I'm on windows 8.1, not sure if it's related to ie11 update...

    0 讨论(0)
  • 2020-11-22 06:34

    Check out http://suhasrathod.wordpress.com/2013/04/29/ie10-css-hacks/

    @media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
           /* IE10-specific styles go here */
    }
    
    0 讨论(0)
  • 2020-11-22 06:35

    CSS for IE10+ and IE9

    @media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
        /* IE10+ styles */
    }
    
    @media screen\0 {
        /* IE8,9,10 styles*/
    }
    
    0 讨论(0)
提交回复
热议问题