亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢查用戶是否使用IE

檢查用戶是否使用IE

MMTTMM 2019-07-03 11:31:57
檢查用戶是否使用IE我用特定的類點擊div來調用像下面這樣的函數。如果用戶正在使用InternetExplorer,我是否可以在啟動函數時檢查它,如果用戶正在使用其他瀏覽器,則可以中止/取消該函數,以便它只為IE用戶運行嗎?這里的用戶都在IE8或更高版本上,所以我不需要介紹IE7和更低版本。如果我能知道他們使用的是哪個瀏覽器,那將是很棒的,但不是必需的。示例功能:$('.myClass').on('click', function(event){     // my function});
查看完整描述

3 回答

?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

使用以下JavaScript方法:

function msieversion() {
    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");

    if (msie > 0) // If Internet Explorer, return version number
    {
        alert(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
    }
    else  // If another browser, return 0
    {
        alert('otherbrowser');
    }

    return false;}

您可以在下面的Microsoft支持站點上找到詳細信息:

如何從腳本中確定瀏覽器版本

最新情況:(IE 11支援)

function msieversion() {

    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))  // If Internet Explorer, return version number
    {
        alert(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
    }
    else  // If another browser, return 0
    {
        alert('otherbrowser');
    }

    return false;}


查看完整回答
反對 回復 2019-07-03
?
夢里花落0921

TA貢獻1772條經驗 獲得超6個贊

加入馬里奧的非常有用的答案。

如果您只想知道瀏覽器是否為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}

更新

我現在就推薦這個。它的可讀性仍然很強,而且代碼要少得多:)

var ua = window.navigator.userAgent;var isIE = /MSIE|Trident/.test(ua);if ( isIE ) {
  //IE specific code goes here}

感謝JohnnyFun在評論中對簡短的答案:)


查看完整回答
反對 回復 2019-07-03
  • 3 回答
  • 0 關注
  • 600 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號