使用IE8 +中的Twitter引导选项卡启用后退按钮处理

Met*_*zed 5 javascript jquery html5 back-button twitter-bootstrap

Bootstrap 2.3.2

我在我的项目中使用Bootstrap中的选项卡来显示内容.在单个页面上有多个具有不同内容的选项卡,在这些选项卡中是用于将用户带到网站上其他页面的链接.

不幸的是,默认情况下,选项卡不支持后退按钮导航,因此如果用户访问另一个页面并点击浏览器的后退按钮,该选项卡将恢复到第一个打开的选项卡,而不是上次查看时的活动选项卡.页.

以下来自FrançoisZaninotto的Javascript 解决了Chrome,Firefox和Safari中的这个问题.François还说它适用于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');
    }
  });
});
Run Code Online (Sandbox Code Playgroud)

`

    <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>
Run Code Online (Sandbox Code Playgroud)

Met*_*zed 1

我多次尝试让上面的代码正常工作,但我不得不放弃,而是实现了以下有效的 javascript: https://gist.github.com/josheinstein/5586469