CSS |仅对Internet Explorer应用样式

CSS |仅对Internet Explorer应用样式,css,internet-explorer,Css,Internet Explorer,目前我正在Internet Explorer 10和11上测试该网站。 目前我有一个样式表,其中有一些样式,应该只适用于访问者使用Internet Explorer浏览网站时。我尝试使用条件注释,但Internet Explorer 10和11不支持这些注释 我想知道是否有人曾经处理过这个问题,可以把我推向正确的方向。非常感谢您的帮助。以下是IE10和IE11的代码。它会将类添加到html标记中 <!--[if !IE]><!--> <script> if (

目前我正在Internet Explorer 10和11上测试该网站。

目前我有一个样式表,其中有一些样式,应该只适用于访问者使用Internet Explorer浏览网站时。我尝试使用条件注释,但Internet Explorer 10和11不支持这些注释


我想知道是否有人曾经处理过这个问题,可以把我推向正确的方向。非常感谢您的帮助。

以下是IE10和IE11的代码。它会将类添加到html标记中

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

<script>
var ua = navigator.userAgent,
      doc = document.documentElement;
if((ua.match(/rv:11.0/i))){
    doc.className = doc.className + " ie11";
  }
</script>

如果(/*@cc_on!@*/false){
document.documentElement.className+='ie10';
}  
var ua=navigator.userAgent,
doc=document.documentElement;
如果((ua.匹配(/rv:11.0/i))){
doc.className=doc.className+“ie11”;
}

不再支持条件注释

Internet Explorer 10标准和怪癖模式中已删除对条件注释的支持,以提高互操作性和对HTML5的遵从性

This Applies to Internet Explorer 10 and later.

有关更多信息,请在head-like中添加此标签

<!--[if IE 7]>
<html class="ie ie7">
<![endif]-->
<!--[if IE 8]>
<html class="ie ie8">
<![endif]-->
<!--[if !(IE 7) & !(IE 8)]><!-->
<html>
<!--<![endif]-->

将用户代理添加到html中,如下所示:

var doc = document.documentElement;
doc.setAttribute('data-useragent', navigator.userAgent);
IE10的用户代理是:

Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)
这将导致:

<html data-useragent="Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)">
因此,IE10中的标题将仅为蓝色

@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
  #myClass { color: red; }
}

请参阅CSS hacks

正如我在OP中所述,internet Explorer 10和11中不支持条件注释。如果($.browser.msie&&$.browser.version==10){$($html”).addClass(“ie10”);},您甚至可以使用like jquery函数
html[data-useragent*='MSIE 10.0'] h1 {
 color: blue;
}
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
  #myClass { color: red; }
}