2013-08-02 16 views
5

引導按鈕處理與Twitter的引導標籤2.3.2啓用早在IE8 +

我在我的項目中使用tabs從引導顯示內容。在單個頁面上有多個標籤,其中包含不同的內容,並且在這些標籤中包含將用戶帶到網站上其他頁面的鏈接。

不幸的是,默認情況下,這些標籤不支持後退按鈕導航,所以如果用戶訪問另一個頁面並點擊瀏覽器的後退按鈕,該選項卡將恢復爲第一個打開的選項卡,而不是當時處於活動狀態的選項卡他們上次查看該頁面。

下面的Javascript從François Zaninotto解決了Chrome,Firefox和Safari中的這個問題。弗朗索瓦還說,它適用於IE8 + - 但在我的測試中,我無法讓它在IE8,IE9或IE10上運行。有誰知道如何讓這個工作在IE8 +?可能嗎?

$(document).ready(function() { 
    // add a hash to the URL when the user clicks on a tab 
    $('a[data-toggle="tab"]').on('click', function(e) { 
    history.pushState(null, null, $(this).attr('href')); 
    }); 
    // navigate to a tab when the history changes 
    window.addEventListener("popstate", function(e) { 
    var activeTab = $('[href=' + location.hash + ']'); 
    if (activeTab.length) { 
     activeTab.tab('show'); 
    } else { 
     $('.nav-pills a:first').tab('show'); 
    } 
    }); 
}); 

`

<ul id="myTab" class="nav nav-pills"> 
       <li class="active"><a href="#tab1" data-toggle="tab">Tab1</a></li> 
       <li><a href="#tab2" data-toggle="tab">Tab2</a></li> 
     <li><a href="#tab3" data-toggle="tab">Tab3</a></li> 

      </ul> 

<div id="myTabContent" class="tab-content"> 
       <div class="tab-pane active" id="tab1"> 
        <h2>Tab 1</h2> 
       <p>Raw denim you probably haven't heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache cliche tempor, williamsburg carles vegan helvetica. Reprehenderit butcher retro keffiyeh dreamcatcher synth. Cosby sweater eu banh mi, qui irure terry richardson ex squid. Aliquip placeat salvia cillum iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui.</p> 
       </div> 
       <div class="tab-pane" id="tab2"> 
        <h2>Tab 2</h2> 
       <p>Food truck fixie locavore, accusamus mcsweeney's marfa nulla single-origin coffee squid. Exercitation +1 labore velit, blog sartorial PBR leggings next level wes anderson artisan four loko farm-to-table craft beer twee. Qui photo booth letterpress, commodo enim craft beer mlkshk aliquip jean shorts ullamco ad vinyl cillum PBR. Homo nostrud organic, assumenda labore aesthetic magna delectus mollit. Keytar helvetica VHS salvia yr, vero magna velit sapiente labore stumptown. Vegan fanny pack odio cillum wes anderson 8-bit, sustainable jean shorts beard ut DIY ethical culpa terry richardson biodiesel. Art party scenester stumptown, tumblr butcher vero sint qui sapiente accusamus tattooed echo park.</p> 
       </div> 
       <div class="tab-pane" id="tab3"> 
        <h2>Tab 3</h2> 
       <p>Food truck fixie locavore, accusamus mcsweeney's marfa nulla single-origin coffee squid. Exercitation +1 labore velit, blog sartorial PBR leggings next level wes anderson artisan four loko farm-to-table craft beer twee. Qui photo booth letterpress, commodo enim craft beer mlkshk aliquip jean shorts ullamco ad vinyl cillum PBR. Homo nostrud organic, assumenda labore aesthetic magna delectus mollit. Keytar helvetica VHS salvia yr, vero magna velit sapiente labore stumptown. Vegan fanny pack odio cillum wes anderson 8-bit, sustainable jean shorts beard ut DIY ethical culpa terry richardson biodiesel. Art party scenester stumptown, tumblr butcher vero sint qui sapiente accusamus tattooed echo park.</p> 
       </div> 
      </div> 
+0

的'history.pushState'是HTML5 。 'popstate'只在IE10中支持。 – putvande

+0

@putvande我不知道如何實現它,但我應該使用[history.js](https://github.com/browserstate/history.js/)?這對我有用嗎? – Metzed

回答

1

我試過很多次,使我上面的代碼工作,但我不得不放棄,而是我實現下面的JavaScript其工作:未在IE8支持 https://gist.github.com/josheinstein/5586469

+1

這是我的要點,讓它在ie8和Chrome上工作:https://gist.github.com/bengm/7535223 –

1

我的實現可能不是解決你的問題,請等待其他答案的最有效的方式。

Persist javascript variables across pages?

使用JavaScript會話存儲標籤的唯一編號,如您目前使用的ID。

tabHistory = "tab" + currentTab; 

返回上一頁時,只需將用戶鏈接到tabHistory。

我對這種技術沒有多少經驗,只使用過一次,但實施起來應該快速簡單,效率也很高。

從我收集的內容來看,如果新窗口/標籤打開,它將不起作用,但後退按鈕也不會起作用。