gpt4 book ai didi

javascript - 后条件注释时代的浏览器检测?

转载 作者:行者123 更新时间:2023-11-30 17:25:15 24 4
gpt4 key购买 nike

Internet Explorer 10+ 不再支持条件注释。好的,太棒了。

这是我需要的:

    (if this browser is Internet Explorer of ANY version) {
//apply IE CSS class here
} else {
//apply non IE CSS class here
}

我已经尝试了所有我能在互联网上找到的东西,但在不影响所有浏览器的情况下没有任何效果。例如,以下代码不起作用,因为“浏览器不是 IE 打印在所有浏览器上:

    <!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>

最佳答案

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 提供脚本。效果很好。

关于javascript - 后条件注释时代的浏览器检测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24400701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com