我通过单击具有特定类的divs调用如下函数。
是否有一种方法,我可以检查启动函数时,如果用户正在使用Internet Explorer和中止/取消它,如果他们正在使用其他浏览器,以便它只运行于IE用户?这里的用户都使用IE8或更高版本,所以我不需要涵盖IE7和更低版本。
如果我能告诉他们使用的浏览器,这将是伟大的,但不是必需的。
示例函数:
$('.myClass').on('click', function(event)
{
// my function
});
我通过单击具有特定类的divs调用如下函数。
是否有一种方法,我可以检查启动函数时,如果用户正在使用Internet Explorer和中止/取消它,如果他们正在使用其他浏览器,以便它只运行于IE用户?这里的用户都使用IE8或更高版本,所以我不需要涵盖IE7和更低版本。
如果我能告诉他们使用的浏览器,这将是伟大的,但不是必需的。
示例函数:
$('.myClass').on('click', function(event)
{
// my function
});
当前回答
我把这段代码放在文档准备函数中,它只在internet explorer中触发。在Internet Explorer 11中测试。
var ua = window.navigator.userAgent;
ms_ie = /MSIE|Trident/.test(ua);
if ( ms_ie ) {
//Do internet explorer exclusive behaviour here
}
其他回答
你可以使用$。浏览器获取名称、供应商和版本信息。
参见http://api.jquery.com/jQuery.browser/
这里有很多答案,我想补充我的意见。IE 11在flexbox上是如此的麻烦(看到所有的问题和不一致在这里),我真的需要一个简单的方法来检查用户是否使用任何IE浏览器(包括11),但不包括Edge,因为Edge实际上非常好。
基于这里给出的答案,我写了一个简单的函数,返回一个全局布尔变量,然后您可以使用它。很容易检查IE。
var isIE;
(function() {
var ua = window.navigator.userAgent,
msie = ua.indexOf('MSIE '),
trident = ua.indexOf('Trident/');
isIE = (msie > -1 || trident > -1) ? true : false;
})();
if (isIE) {
alert("I am an Internet Explorer!");
}
这样,您只需查找一次,并将结果存储在一个变量中,而不必在每次函数调用时获取结果。(据我所知,你甚至不需要等待文档准备好执行这段代码,因为用户代理与DOM无关。)
我在2020年登陆这个页面,我看到直到IE5所有的userAgent字符串都有Trident,我不确定他们是否有任何改变。所以只检查userAgent中的Trident对我来说是有效的。
var isIE = navigator.userAgent.indexOf('Trident') > -1;
你可以使用navigator对象来检测user-navigator,你不需要jquery,下面的4个注释已经包括在内,所以这个代码片段可以正常工作
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent) || navigator.userAgent.indexOf("Trident/") > -1 ){
// Do stuff with Internet-Exploders ... :)
}
http://www.javascriptkit.com/javatutors/navigator.shtml
如果你只想知道浏览器是否是IE,你可以这样做:
var isIE = false;
var ua = window.navigator.userAgent;
var old_ie = ua.indexOf('MSIE ');
var new_ie = ua.indexOf('Trident/');
if ((old_ie > -1) || (new_ie > -1)) {
isIE = true;
}
if ( isIE ) {
//IE specific code goes here
}
更新1:一个更好的方法
我现在就推荐这个。它仍然是非常可读的,并且代码更少:)
var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident/.test(ua);
if ( isIE ) {
//IE specific code goes here
}
感谢JohnnyFun在评论中给出的简短答案:)
更新2:在CSS中测试IE
首先,如果可以的话,你应该使用@supports语句而不是JS来检查浏览器是否支持某个CSS特性。
.element {
/* styles for all browsers */
}
@supports (display: grid) {
.element {
/* styles for browsers that support display: grid */
}
}
(注意IE根本不支持@supports,并且会忽略任何放在@supports语句中的样式。)
如果这个问题不能通过@supports解决,那么你可以这样做:
// JS
var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident/.test(ua);
if ( isIE ) {
document.documentElement.classList.add('ie')
}
/* CSS */
.element {
/* styles that apply everywhere */
}
.ie .element {
/* styles that only apply in IE */
}
(注意:classList对于JS来说相对较新,我认为,在IE浏览器之外,它只能在IE11中工作。也可能是IE10。)
如果你在项目中使用SCSS (Sass),这可以简化为:
/* SCSS (Sass) */
.element {
/* styles that apply everywhere */
.ie & {
/* styles that only apply in IE */
}
}
更新3:添加Microsoft Edge(不推荐)
如果您还想将Microsoft Edge添加到列表中,您可以执行以下操作。但是我不推荐它,因为Edge是一个比IE强大得多的浏览器。
var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident|Edge\//.test(ua);
if ( isIE ) {
//IE & Edge specific code goes here
}