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

前端 未结 25 2619
谎友^
谎友^ 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:47

    I've written a small, vanilla JavaScript plugin called Layout Engine, which allows you to feature detect IE 10 (and every other browser), in a simple way that cannot be faked, unlike user agent sniffing.

    It adds the rendering engine name as a class on the html tag and returns a JavaScript object containing the vendor and version (where appropriate)

    Check out my blog post: http://mattstow.com/layout-engine.html and get the code on GitHub: https://github.com/stowball/Layout-Engine

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

    If you don't mind targeting of IE10 and above and non IE browsers, you can use this conditional comment:

    <!--[if gt IE 9]><!--> Your code here. <!--<![endif]-->
    

    Derived from http://www.paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither

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

    I wouldn't use JavaScript navigator.userAgent or $.browser (which uses navigator.userAgent) since it can be spoofed.

    To target Internet Explorer 9, 10 and 11 (Note: also the latest Chrome):

    @media screen and (min-width:0\0) { 
        /* Enter CSS here */
    }
    

    To target Internet Explorer 10:

    @media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
        /* IE10+ CSS here */
    }
    

    To target Edge Browser:

    @supports (-ms-accelerator:true) {
      .selector { property:value; } 
    }
    

    Sources:

    • Moving Internet Explorer specific CSS into @media blocks
    • How to Target Internet Explorer 10 and 11 in CSS
    • CSS Hacks for Windows 10 and Microsoft’s Edge Web Browser
    0 讨论(0)
  • 2020-11-22 06:49

    This answer got me 90% of the way there. I found the rest of my answer on the Microsoft site here.

    The code below is what I'm using to target all ie by adding a .ie class to <html>

    Use jQuery (which deprecated .browser in favor of user agents in 1.9+, see http://api.jquery.com/jQuery.browser/) to add an .ie class:

    // ie identifier
    $(document).ready(function () {
      if (navigator.appName == 'Microsoft Internet Explorer') {
        $("html").addClass("ie");
      }
    });
    
    0 讨论(0)
  • 2020-11-22 06:52

    You can use PHP to add a stylesheet for IE 10

    Like:

    if (stripos($_SERVER['HTTP_USER_AGENT'], 'MSIE 10')) {
        <link rel="stylesheet" type="text/css" href="../ie10.css" />
    }
    
    0 讨论(0)
  • 2020-11-22 06:53

    If you really have to, you can make conditional comments work by adding the following line to <head>:

    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9">
    

    Source

    0 讨论(0)
提交回复
热议问题