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

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

    Both solutions posted here (with slight modifications) work:

    <!--[if !IE]><!--><script>if(/*@cc_on!@*/false){document.documentElement.className='ie10';}</script><!--<![endif]-->
    

    or

    <script>if(Function('/*@cc_on return 10===document.documentMode@*/')()){document.documentElement.className='ie10';}</script>
    

    You include either of the above lines inside of head tag of your html page before your css link. And then in css file you specify your styles having "ie10" class as a parent:

    .ie10 .myclass1 { }
    

    And voilà! - other browsers stay intact. And you don't need jQuery. You can see the example how I implemented it here: http://kardash.net.

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

    clipBoardData is a function that is only available in IE, so if you are seeking to target all IE versions you can use

    <script type="text/javascript">
    if (window.clipboardData)
                alert("You are using IE!");
    </script>
    
    0 讨论(0)
  • 2020-11-22 06:39

    modern solution for css

    html[data-useragent*='MSIE 10.0'] .any {
      your-style: here;
    }
    
    0 讨论(0)
  • 2020-11-22 06:42

    If you want to target IE 10 with Vanilla JavaScript, you might want to try CSS property detection:

    if (document.body.style.msTouchAction != undefined) {
      document.body.className = 'ie10';
    }
    

    CSS properties

    Instead of msTouchAction you can also use one of these CSS properties, because they are currently only available in IE 10. But this might change in the future.

    • msTouchAction
    • msWrapFlow
    • msWrapMargin
    • msWrapThrough
    • msScrollLimit
    • msScrollLimitXMin
    • msScrollLimitYMin
    • msScrollLimitXMax
    • msScrollLimitYMax
    • msFlexbox
    • msFlex
    • msFlexOrder

    Test page

    I've put together a test page with all properties on CodePen.

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

    Conditionizr (see docs) will add browser CSS classes to your html element, including ie10.

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

    use babel or typescript for convert this code js

    const browser = () => {
        // "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3576.0 Safari/537.36"
        // "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.17 Safari/537.36"
        // "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763"
        // "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0"
        // "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; rv:11.0) like Gecko"
        // "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)"
        // "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)"
        // "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)"
        // "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)"
        const _userAgent = navigator.userAgent;
        const br = {
            "Chrome": "Chrome",
            "Edge": "Edge",
            "Firefox": "Firefox",
            ".NET CLR": "Internet Explorer 11",
        };
        const nobr = {
            "MSIE 10.0": "Internet Explorer 10",
            "MSIE 9.0": "Internet Explorer 9",
            "MSIE 8.0": "Internet Explorer 8",
            "MSIE 7.0": "Internet Explorer 7"
        };
        let thisBrow = "Unknown";
        for (const keys in br) {
            if (br.hasOwnProperty(keys)) {
                if (_userAgent.includes(keys)) {
    
                    thisBrow = br[keys];
    
                    for (const key in nobr) {
                        if (_userAgent.includes(key)) {
                            thisBrow = nobr[key];
                        }
                    }
    
                }
            }
        }
    
        return thisBrow;
    };

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