2016-02-26 64 views
0

中不起作用我在我的網頁上有兩個選項卡:付款和語句。 我使用以下代碼在我重新加載相同頁面時自動切換到選定選項卡。重新加載所選標籤在IE

$('#advancedSearchTabs a').click(function(e) { 
     e.preventDefault(); 
     $(this).tab('show'); 
    }); 

    // store the currently selected tab 
    $("ul.nav-tabs > li > a").on("shown.bs.tab", function(e) { 
     var id = $(e.target).attr("href").substr(1); 
     window.location.hash = id; 
    }); 

    // on load of the page, switch to the currently selected tab 
    var hash = window.location.hash; 
    $('#advancedSearchTabs a[href="' + hash + '"]').tab('show'); 

這個工作在每一個瀏覽器,除了IE 11.我總是跳回到第一個標籤。我怎樣才能使這項工作在Internet Explorer中

回答

0

請確保您正在爲IE正確獲取事件目標。

$("ul.nav-tabs > li > a").on("shown.bs.tab", function(e) { 
    var target = e.target || e.srcElement; //srcElement for IE 
    var id = target.getAttribute("href"); //No need for jQuery here 
    history.pushState('','',id); //Try pushState 
}); 

var hash = (location.href.split("#")[1] || ""); //Crossbrowser compatible 
$('#advancedSearchTabs a[href$="' + hash + '"]').tab('show'); //Add $ to href attribute ends with "hash" 

JavaScript event.target and event.srcElement

+0

我加入你的額外的代碼行,但沒有奏效。 – Lumpi

+0

如果在新行之後使用'console.log(target)',控制檯中有什麼? – alexP

+0
相關問題