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

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

    I found a solution on this site where someone had a valuable comment:

    The solution is:

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

    It

    • doesn’t need conditional comments;
    • works even if comment stripping compression/processing;
    • no ie10 class added in Internet Explorer 11;
    • more likely to work as intended with Internet Explorer 11 running in Internet Explorer 10 compatibility mode;
    • doesn’t need standalone script tag (can just be added to other JavaScript code in the head).
    • doesn't need jQuery to test

提交回复
热议问题