Internet Explorer 10+不再支持条件注释。好的,太棒了。
这是我需要的:
(if this browser is Internet Explorer of ANY version) {
//apply IE CSS class here
} else {
//apply non IE CSS class here
}我已经尝试了所有我能在互联网上找到的东西,没有一个不影响所有浏览器的。例如,以下代码不起作用,因为"Browser is nott IE is printed on all Browser:
<!DOCTYPE html>
<!--[if lte IE 8]><html class="ie8 no-js" lang="en">IE 8-10<![endif]-->
<!--[if lte IE 10]><html class="ie10 no-js" lang="en">ID 8-<![endif]-->
<script>
var isIE = (navigator.userAgent.indexOf("MSIE") != -1);
if (!isIE) {
document.write("<html class='not-ie no-js' lang='en'>");
document.write("Browser is nott IE.");
}
</script>发布于 2014-06-27 02:36:55
navigator.sayswho= (function(){
var ua= navigator.userAgent, tem,
M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if(/trident/i.test(M[1])){
tem= /\brv[ :]+(\d+)/g.exec(ua) || [];
return 'IE '+(tem[1] || '');
}
if(M[1]=== 'Chrome'){
tem= ua.match(/\bOPR\/(\d+)/)
if(tem!= null) return 'Opera '+tem[1];
}
M= M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
if((tem= ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]);
return M.join(' ');
})();代码将返回浏览器的名称。您可以根据返回值执行条件语句。
感谢u/kennebec的脚本。效果很好。
发布于 2014-06-27 04:12:40
虽然功能检测通常是区分支持和非支持浏览器版本的最经济的方法,但检测IE --及其版本--实际上是小菜一碟:
var uA = navigator.userAgent;
var browser = null;
var ieVersion = null;
var htmlTag = document.documentElement;
if (uA.indexOf('MSIE 6') >= 0) {
browser = 'IE';
ieVersion = 6;
}
if (uA.indexOf('MSIE 7') >= 0) {
browser = 'IE';
ieVersion = 7;
}
if (document.documentMode) { // as of IE8; strictly IE proprietary
browser = 'IE';
ieVersion = document.documentMode;
}
// Using it can be done like this:
if (browser == 'IE' && ieVersion == 11)
htmlTag.className += ' ie11';一个是在较低的模式/视图中捕获较高的is,以及使用此脚本,包括兼容性。我包含了版本部分,因为任何认真的web开发人员迟早都会遇到IE版本的差异。
https://stackoverflow.com/questions/24400701
复制相似问题