【问题标题】:Make Bootstrap tab Active on the bases of URL link基于 URL 链接使 Bootstrap 选项卡处于活动状态
【发布时间】:2015-12-28 09:50:37
【问题描述】:

我有引导选项卡,我想在 URL 链接的基础上激活选项卡。

例如:

xyz.xxx/index#tab2 应该在页面加载时使 tab2 处于活动状态

这是我目前的代码

<div class="tab-wrap">
  <div class="parrent pull-left">
    <ul class="nav nav-tabs nav-stacked">
      <li class="active"><a href="#tab1" data-toggle="tab" class="analistic-01">Tab 1</a></li>
      <li class=""><a href="#tab2" data-toggle="tab" class="analistic-02">Tab 2</a></li>
      <li class=""><a href="#tab3" data-toggle="tab" class="analistic-03">Tab 3</a></li>
    </ul>
  </div>
  <div class="tab-content">
    <div class="tab-pane active in" id="tab1">
      <p> hello 1</p>
    </div>

    <div class="tab-pane" id="tab2">
      <p> hello 2</p>
    </div>

    <div class="tab-pane" id="tab3">
      <p>Hello 3</p>
    </div>
  </div> <!--/.tab-content-->
</div><!--/.tab-wrap-->

默认情况下,它使 tab1 处于活动状态,因此在我的情况下,可能会根据我的 URL 链接默认使第二个选项卡处于活动状态。

假设如果我将#tab2 放在 URL 中,那么它应该在页面加载时默认使 tab2 处于活动状态。

【问题讨论】:

  • 您想根据 URL 激活标签页,例如如果您在 url 中传递#tab2,那么 ID 为 tab2 的标签页应该是活动的?
  • 是的。像这样的

标签: jquery twitter-bootstrap tabs


【解决方案1】:

您可以使用类似这样的 jquery 来实现它。 您的网址,例如“www.myurl.com/page#tab1”;

var url = window.location.href;

从 url 链接获取要激活的选项卡。

 var activeTab = url.substring(url.indexOf("#") + 1);

删除旧的活动标签类

 $(".tab-pane").removeClass("active in");

将活动类添加到新标签

$("#" + activeTab).addClass("active in");

或者得到activeTab后直接打开tab。

$('a[href="#'+ activeTab +'"]').tab('show')

【讨论】:

  • 或者直接去掉中间人,使用document.location.hash获取访问URL末尾的hash
  • 这个问题的公认最佳实践是什么?
【解决方案2】:
  1. 从 URL 获取哈希并显示活动标签
  2. 将当前哈希设置为 URL

您可以使用此代码:

$(function(){
  var hash = window.location.hash;
  hash && $('ul.nav.nav-pills a[href="' + hash + '"]').tab('show'); 
  $('ul.nav.nav-pills a').click(function (e) {
     $(this).tab('show');
     $('body').scrollTop();
     window.location.hash = this.hash;
  });
});

【讨论】:

  • 在 Bootstrap 4.3 上工作。
  • 也适用于 Bottstrap 3.3。我删除了选择器中的“.nav-pills”。
  • 我用 Bootstrap 5 为这个用例尝试了很多方法,这个方法很有帮助,谢谢。我尝试过的大多数其他人都不适用于新版本的 BS。唯一需要注意的是 BS5 不需要 jQuery,因此总体而言,纯 JS 选项可能更好。
【解决方案3】:

根据您设置 JS 的方式,您应该能够做到这一点:

www.myurl.com/page#tab1

www.myurl.com/page#tab2

www.myurl.com/page#tab3

其中#tab1#tab2#tab3 等于标签的id

编辑

请看这里: Twitter Bootstrap - Tabs - URL doesn't change

【讨论】:

    【解决方案4】:

    您可以向&lt;ul class="nav nav-tabs nav-stacked" id="myTab"&gt; 添加一个 id,然后使用以下 javascript 代码:

    $(document).ready(() => {
      let url = location.href.replace(/\/$/, "");
    
      if (location.hash) {
        const hash = url.split("#");
        $('#myTab a[href="#'+hash[1]+'"]').tab("show");
        url = location.href.replace(/\/#/, "#");
        history.replaceState(null, null, url);
        setTimeout(() => {
          $(window).scrollTop(0);
        }, 400);
      } 
    
      $('a[data-toggle="tab"]').on("click", function() {
        let newUrl;
        const hash = $(this).attr("href");
        if(hash == "#home") {
          newUrl = url.split("#")[0];
        } else {`enter code here`
          newUrl = url.split("#")[0] + hash;
        }
        newUrl += "/";
        history.replaceState(null, null, newUrl);
      });
    });
    

    更多详情follow this link

    【讨论】:

      【解决方案5】:

      如果你只是想基于url触发,你也可以按照如下方式进行

      $(function(){
         var hash = window.location.hash;
         if(hash != ''){
            $("a[href='"+url+"']").click();
         }
      })
      

      【讨论】:

        【解决方案6】:
        $(function(){
            var url = window.location.href;
            var activeTab = url.substring(url.indexOf("#") + 1);
            if($.trim(activeTab) == 'Exhibitor') // check hash tag name for prevent error
            {
                $(".tab-pane").removeClass("active in");
                $("#" + activeTab).addClass("active in");
                $('a[href="#'+ activeTab +'"]').tab('show');
            }
        });
        

        【讨论】:

          【解决方案7】:

          使用来自URL APIURL() 构造函数:

          URL {
            href: "https://stackoverflow.com/posts/67036862/edit",
            origin: "https://stackoverflow.com",
            protocol: "https:",
            username: "",
            password: "",
            host: "stackoverflow.com",
            hostname: "stackoverflow.com",
            port: "",
            pathname: "/posts/67036862/edit",
            search: ""
          }
          

          根据 URL 链接使 Bootstrap 选项卡处于活动状态:

          $(function() {
            /**
             * Make Bootstrap tab Active on the bases of URL link
             */
            const loc = new URL(window.location.href) || null
            if (loc !== null) {
              console.debug(loc)
              if (loc.hash !== '') {
                $('.tab-pane').removeClass('active in')
                $(loc.hash).addClass('active in')
                $(`a[href="${ loc.hash }"]`).tab('show')
              }
            }
          })
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-01-19
            • 2012-08-09
            • 2014-07-12
            • 1970-01-01
            • 2020-06-04
            相关资源
            最近更新 更多