为什么IE9不覆盖CSS类?

时间:2019-02-19 13:32:38

标签: javascript html internet-explorer-9 conditional-comments

我有一段HTML代码,我们正在其中为IE9,IE10和IE11应用特殊的CSS。

<!doctype html>
     <!--[if IE 9]><html data-placeholder-focus="false" lang="{%=user_locale_html}}" dir="ltr" class="ie9 lt-ie10 lt-ie11 lt-ie12 gt-ie8 gt-ie7 gt-ie6"><![endif]-->

     <!--[if !(IE)]><!--><html lang="{%=user_locale_html}}" dir="{%=dir}}">

<script>

  var ua = window.navigator.userAgent;

  if (ua.indexOf("Trident/7.0") > 0) 
    document.documentElement.className='ie11 lt-ie12 gt-ie10 gt-ie9 gt-ie8 gt-ie7 gt-ie6';
  else if (ua.indexOf("Trident/6.0") > 0)
    document.documentElement.className='ie10 lt-ie11 lt-ie12 gt-ie9 gt-ie8 gt-ie7 gt-ie6';

  if(/*@cc_on!@*/false){
    document.documentElement.className='gt-ie11 gt-ie10 gt-ie9 gt-ie8 gt-ie7 gt-ie6';
  }

</script>
<!--<![endif]-->
</html>

注意代码if(/*@cc_on!@*/false) {}

当我们拥有userAgant=Trident/6.0.时,此代码将覆盖IE10中应用的css类(这使我无法覆盖ie10类。

但是我的问题是,当浏览器是IE9时,为什么这段代码没有覆盖这些类?

我知道代码中不需要@cc_on相关的内容,但是我很好奇知道它的行为方式是否不同。

谢谢!

1 个答案:

答案 0 :(得分:1)

可能您的代码无法识别IE 9,这就是为什么CSS类无法被覆盖的原因。

我建议您尝试参考下面的代码示例,该示例可以找到IE 8,IE 9,IE 10,IE 11。

<!DOCTYPE html>
<html>
<head>
	<title>Page Title</title>
	<script>
	function GetIEVersion() {
  var sAgent = window.navigator.userAgent;
  var Idx = sAgent.indexOf("MSIE");

  // If IE, return version number.
  if (Idx > 0) 
    return parseInt(sAgent.substring(Idx+ 5, sAgent.indexOf(".", Idx)));

  // If IE 11 then look for Updated user agent string.
  else if (!!navigator.userAgent.match(/Trident\/7\./)) 
    return 11;

  else
    return 0; //It is not IE
}

if (GetIEVersion() > 0) 
   alert("This is IE " + GetIEVersion());
else 
   alert("This is not IE.");
	</script>
</head>
<body>

</body>
</html>

输出:

enter image description here

此外,您可以尝试根据需要进行修改,以帮助您解决问题。