如何仅针对 Internet Explorer 10,用于某些情况,例如特定于 Internet Explorer 的 CSS 或 Internet Explorer 特定的 JavaScript 代码?

如何仅针对 Internet Explorer 10,用于某些情况,例如特定于 Internet Explorer 的 CSS 或 Internet Explorer 特定的 JavaScript 代码?

我试过这个,但它不起作用:

<!--[if IE 10]>    <html class="no-js ie10" lang="en"> <![endif]-->
<!--[if !IE]><!--> <html lang="en" class="no-js"> <!--<![endif]-->

Internet Explorer 10 忽略条件注释,并使用 .<html lang="en" class="no-js"><html class="no-js ie10" lang="en">


答案 1

我不会使用JavaScript或$ .browser(使用),因为它可以被欺骗。navigator.userAgentnavigator.userAgent

要定位到 Internet Explorer 9、10 和 11(注意:也是最新的 Chrome):请执行以下操作:

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

要以 Internet Explorer 10 为目标,请执行以下操作:

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

要定位边缘浏览器:

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

来源:


答案 2

在这个网站上找到了一个解决方案,有人有一个有价值的评论

解决方案是:

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

  • 不需要条件注释;
  • 即使注释剥离压缩/处理也有效;
  • 在 Internet Explorer 11 中没有添加 ie10 类;
  • 更有可能在Internet Explorer 10兼容模式下运行的Internet Explorer 11中按预期工作;
  • 不需要独立的脚本标签(可以添加到头部的其他JavaScript代码中)。
  • 不需要 jQuery 来测试