在IE8+中使用Twitter Bootstrap选项卡时启用后退按钮处理

5

Bootstrap 2.3.2

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

不幸的是,默认情况下,选项卡不支持后退按钮导航,因此,如果用户访问另一个页面并按浏览器的后退按钮,选项卡将恢复到第一个打开的选项卡,而不是他们最后查看页面时处于活动状态的选项卡。

以下Javascript代码来自François Zaninotto,解决了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');
    }
  });
});

`

    <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>

history.pushState是HTML5的一部分,不支持IE8。popstate仅在IE10中受支持。 - putvande
@putvande 我不确定如何实现,但我应该使用history.js吗?那对我有用吗? - Metzed
2个回答

1

我实现的方法可能不是解决您问题的最有效方式,请等待其他答案。

如何跨页面保留javascript变量?

使用JavaScript会话来存储标签的唯一编号,就像您当前用于ID一样。

tabHistory = "tab" + currentTab;

返回前一页时,只需将用户链接到tabHistory即可。

我对这种技术没有太多经验,只用过一次,但它应该很快、简单实现,也非常高效。

据我了解,如果打开了新窗口/标签页,它将无法工作,但是后退按钮也不会,所以这并不重要。


1

1
这是我的要点,以便在ie8和Chrome上运行:https://gist.github.com/bengm/7535223 - Ben Morris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接