简体   繁体   English

Bootstrap如何直接链接到特定选项卡中的锚标签

[英]Bootstrap how to directly link to an anchor tag within a specific tab

I'm using a bootstrap v3 and want to make a link to an anchor tag within a specific tab. 我正在使用bootstrap v3,并想链接到特定标签内的锚标记。 I found a way to link to a specific tab like this way: 我发现了一种链接到特定标签的方法,如下所示:

http://www.example.com/faq.html#tab2 http://www.example.com/faq.html#tab2

Below is the code I used to get this work. 下面是我用来完成这项工作的代码。

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

However, I want to jump to an anchor tag within this tab, so I made a link like below but it won't work. 但是,我想跳到该选项卡中的锚标记,因此我进行了如下所示的链接,但它不起作用。

faq.html#tab2#topic2-3

I believe 2 hashtag is making a problem? 我相信2个标签正在出问题? Is there a way to make a direct link to the anchor tag in a specific tab? 有没有办法直接链接到特定选项卡中的锚标记? Maybe like this way? 也许是这样的?

faq.html?tab=2#topic2-3

Thank you for the help. 感谢您的帮助。

In order to use anchors, the fact that you are using bootstrap doesn't matter. 为了使用锚点,使用引导程序这一事实无关紧要。 You need to properly give the name to the anchor/title that you want to use. 您需要正确地将名称提供给您要使用的锚点/标题。 In order to do that you can do 为了做到这一点,你可以做

<a name="tab2">Title Text</a>

Now, when you go to 现在,当你去

/myPage.html#tab2

you will get to where you want. 你会到达你想要的地方。 Read here for more info: http://help.typepad.com/anchor-tags.html 在此处阅读更多信息: http : //help.typepad.com/anchor-tags.html

Before you reach the anchor, if you want to go to a specific tab, you should find it by using a get request, which is as you stated, this way you aren't trying to find two anchors, which doesn't make sense to the browser. 在你到达锚点之前,如果你想去一个特定的标签,你应该通过使用get请求找到它,就像你说的那样,这样你就不会试图找到两个锚点,这是没有意义的浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM