twitter-bootstrap 链接到特定选项卡 Bootstrap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26352181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 21:42:27  来源:igfitidea点击:

Link to specific tab Bootstrap

javascriptjqueryhtmldjangotwitter-bootstrap

提问by Alexandre Lara

I'm developing a site with Django Framework and I'm trying to create a way for when a user access a link like http://www.example.com/site/#users_ratingit opens a specific tab in the page.

我正在使用 Django 框架开发一个站点,我正在尝试创建一种方法,当用户访问像http://www.example.com/site/#users_rating这样的链接时,它会在页面中打开一个特定的选项卡。

I tried the following code that I found on the Internet (I'm new in JavaScript/JQuery), it isn't working:

我尝试了在 Internet 上找到的以下代码(我是 JavaScript/JQuery 的新手),但它不起作用:

<script type="text/javascript">
    $(function() {
      // Javascript to enable link to tab
      var url = document.location.toString();
      if (url.match('#')) {
        $('.nav-tabs a[href=#'+url.split('#')[1]+']').tab('show') ;
      }

      // Change hash for page-reload
      $('a[data-toggle="tab"]').on('show.bs.tab', function (e) {
        window.location.hash = e.target.hash;
      });
    });
</script>

My template uses BootStrap 3, here is the HTML code (with some Django tags):

我的模板使用 BootStrap 3,这里是 HTML 代码(带有一些 Django 标签):

<div class="col-md-12" style="margin: 0 auto;">
        <section class="panel">
            <header class="panel-heading tab-bg-dark-navy-blue">
                <ul class="nav nav-tabs nav-justified ">
                    <li class="active">
                        <a data-toggle="tab" href="#overview">
                            {% trans "Overview" %}
                        </a>
                    </li>
                    <li class="">
                        <a data-toggle="tab" href="#timeline">
                            {% trans "Timeline" %}
                        </a>
                    </li>
                    <li class="">
                        <a data-toggle="tab" href="#users_rating">
                            {% trans "Users Ratings" %} ({{ ptc.userrating.count }})
                        </a>
                    </li>
                    <li class="">
                        <a data-toggle="tab" href="#rating">
                            {% trans "Our Rating" %}
                        </a>
                    </li>
                </ul>
            </header>
            <div class="panel-body">
                <div class="tab-content tasi-tab">


                    <!-- Overview Tab-Pane -->
                    <div id="overview" class="tab-pane active">
                        {% include 'overview.html' %}
                    </div>

                    <!-- Timeline Tab-Pane -->

                    <div id="timeline" class="tab-pane">
                        {% include 'timeline.html' %}
                    </div>

                    <!-- User Rating Tab-Pane -->

                    <div id="users_rating" class="tab-pane">
                        {% include 'users_rating.html' %}
                    </div>

                    <!-- Our Rating Tab-Pane -->

                    <div id="rating" class="tab-pane">
                        {% include 'rating.html' %}
                    </div>


                </div>
            </div>

        </section>
    </div>

How can I open an specific tab according to an URL in my site?

如何根据我网站中的 URL 打开特定选项卡?

回答by Fizer Khan

Following code works for me

以下代码对我有用

HTML

HTML

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery.min.js"></script>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" type="text/css" />
  <script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
  <script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<div class="col-md-12" style="margin: 0 auto;">
        <section class="panel">
            <header class="panel-heading tab-bg-dark-navy-blue">
                <ul class="nav nav-tabs nav-justified ">
                    <li class="active">
                        <a data-toggle="tab" href="#overview">
                            {% trans "Overview" %}
                        </a>
                    </li>
                    <li class="">
                        <a data-toggle="tab" href="#timeline">
                            {% trans "Timeline" %}
                        </a>
                    </li>
                    <li class="">
                        <a data-toggle="tab" href="#users_rating">
                            {% trans "Users Ratings" %} ({{ ptc.userrating.count }})
                        </a>
                    </li>
                    <li class="">
                        <a data-toggle="tab" href="#rating">
                            {% trans "Our Rating" %}
                        </a>
                    </li>
                </ul>
            </header>
            <div class="panel-body">
                <div class="tab-content tasi-tab">


                    <!-- Overview Tab-Pane -->
                    <div id="overview" class="tab-pane active">
                        {% include 'overview.html' %}
                    </div>

                    <!-- Timeline Tab-Pane -->

                    <div id="timeline" class="tab-pane">
                        {% include 'timeline.html' %}
                    </div>

                    <!-- User Rating Tab-Pane -->

                    <div id="users_rating" class="tab-pane">
                        {% include 'users_rating.html' %}
                    </div>

                    <!-- Our Rating Tab-Pane -->

                    <div id="rating" class="tab-pane">
                        {% include 'rating.html' %}
                    </div>
                </div>
            </div>

        </section>
    </div>
</body>
</html>

JS

JS

    <script type="text/javascript">
        $(function() {
          // Javascript to enable link to tab
          var hash = document.location.hash;
          if (hash) {
            console.log(hash);
            $('.nav-tabs a[href='+hash+']').tab('show');
          }

          // Change hash for page-reload
          $('a[data-toggle="tab"]').on('show.bs.tab', function (e) {
            window.location.hash = e.target.hash;
          });
        });
    </script>

回答by Dt23

Thanks a bunch. With newer versions of JQuery (mine=3.3.1) you need to make a little alteration:

谢谢一堆。使用较新版本的 JQuery(我的=3.3.1),您需要做一些改动:

<script>
     $(function() {
       // Javascript to enable link to tab
       var hash = document.location.hash;
       if (hash) {
         console.log(hash);
         $('.nav-tabs a[href=\'+hash+']').tab('show');
       }

       // Change hash for page-reload
       $('a[data-toggle="tab"]').on('show.bs.tab', function (e) {
         window.location.hash = e.target.hash;
       });
     });
 </script>

Hope this saves someone the hour I lost

希望这可以节省我失去的时间