我通过单击具有特定类的divs调用如下函数。

是否有一种方法,我可以检查启动函数时,如果用户正在使用Internet Explorer和中止/取消它,如果他们正在使用其他浏览器,以便它只运行于IE用户?这里的用户都使用IE8或更高版本,所以我不需要涵盖IE7和更低版本。

如果我能告诉他们使用的浏览器,这将是伟大的,但不是必需的。

示例函数:

$('.myClass').on('click', function(event)
{
    // my function
});

当前回答

对于任何版本的Internet Explorer都返回true:

function isIE(userAgent) {
  userAgent = userAgent || navigator.userAgent;
  return userAgent.indexOf("MSIE ") > -1 || userAgent.indexOf("Trident/") > -1 || userAgent.indexOf("Edge/") > -1;
}

userAgent参数是可选的,默认为浏览器的用户代理。

其他回答

对于任何版本的Internet Explorer都返回true:

function isIE(userAgent) {
  userAgent = userAgent || navigator.userAgent;
  return userAgent.indexOf("MSIE ") > -1 || userAgent.indexOf("Trident/") > -1 || userAgent.indexOf("Edge/") > -1;
}

userAgent参数是可选的,默认为浏览器的用户代理。

方法1: 美元。浏览器在jQuery 1.3版中已弃用,并在1.9版中被移除

if ( $.browser.msie) {
  alert( "Hello! This is IE." );
}

方法2: 使用条件注释

<!--[if gte IE 8]>
<p>You're using a recent version of Internet Explorer.</p>
<![endif]-->

<!--[if lt IE 7]>
<p>Hm. You should upgrade your copy of Internet Explorer.</p>
<![endif]-->

<![if !IE]>
<p>You're not using Internet Explorer.</p>
<![endif]>

方法03:

 /**
 * Returns the version of Internet Explorer or a -1
 * (indicating the use of another browser).
 */
function getInternetExplorerVersion()
{
    var rv = -1; // Return value assumes failure.

    if (navigator.appName == 'Microsoft Internet Explorer')
    {
        var ua = navigator.userAgent;
        var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat( RegExp.$1 );
    }

    return rv;
}

function checkVersion()
{
    var msg = "You're not using Internet Explorer.";
    var ver = getInternetExplorerVersion();

    if ( ver > -1 )
    {
        if ( ver >= 8.0 ) 
            msg = "You're using a recent copy of Internet Explorer."
        else
            msg = "You should upgrade your copy of Internet Explorer.";
    }

    alert( msg );
}

方法04: 使用JavaScript/手动检测

/*
     Internet Explorer sniffer code to add class to body tag for IE version.
     Can be removed if your using something like Modernizr.
 */
 var ie = (function ()
 {

     var undef,
     v = 3,
         div = document.createElement('div'),
         all = div.getElementsByTagName('i');

     while (
     div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i>< ![endif]-->',
     all[0]);

     //append class to body for use with browser support
     if (v > 4)
     {
         $('body').addClass('ie' + v);
     }

 }());

参考链接

试着这样做

if ($.browser.msie && $.browser.version == 8) {
    //my stuff

}

如果你只想知道浏览器是否是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
}

您可以检测到所有Internet Explorer(最近测试的版本12)。

<script>
    var $userAgent = '';
    if(/MSIE/i['test'](navigator['userAgent'])==true||/rv/i['test'](navigator['userAgent'])==true||/Edge/i['test'](navigator['userAgent'])==true){
       $userAgent='ie';
    } else {
       $userAgent='other';
    }

    alert($userAgent);
</script>

请看这里https://jsfiddle.net/v7npeLwe/