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

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

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

示例函数:

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

当前回答

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

其他回答

方法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);
     }

 }());

参考链接

你可以使用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

@SpiderCode的解决方案不能在ie11中工作。以下是我在代码中使用的最佳解决方案,我需要对特定功能进行浏览器检测。

IE11不再报告为MSIE,根据这个更改列表,这是为了避免错误检测。

如果你真的想知道它是IE,你可以做的是在用户代理if导航器中检测Trident/字符串。appName返回Netscape,类似于(未测试的);

感谢这个答案

function isIE()
{
  var rv = -1;
  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 );
  }
  else if (navigator.appName == 'Netscape')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv == -1 ? false: true;
}

我在2020年登陆这个页面,我看到直到IE5所有的userAgent字符串都有Trident,我不确定他们是否有任何改变。所以只检查userAgent中的Trident对我来说是有效的。

var isIE = navigator.userAgent.indexOf('Trident') > -1;

JavaScript检测ie或Edge版本的功能

function ieVersion(uaString) {
  uaString = uaString || navigator.userAgent;
  var match = /\b(MSIE |Trident.*?rv:|Edge\/)(\d+)/.exec(uaString);
  if (match) return parseInt(match[2])
}