我有一段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) {}
当我们有IE10时,这段代码正在重写在userAgant=Trident/6.0.中应用的css类(这给我带来了覆盖ie10类的问题。
但是我的问题是,为什么当浏览器是IE9?时,这段代码不覆盖类?
我知道代码中不需要与@cc_on相关的东西,但我很想知道它的行为是如何不同的。
谢谢!
发布于 2019-02-20 05:28:22
可能您的代码没有标识IE9,这就是CSS类没有被覆盖的原因。
我建议你试着参考下面的代码示例,它可以找到IE8,IE9,IE10,IE11。
<!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>
输出:

此外,您可以尝试根据您的需求对其进行修改,这样可以帮助您解决问题。
https://stackoverflow.com/questions/54767502
复制相似问题