简体   繁体   English

引导程序选项卡的URL,未指向正确的选项卡

[英]URL for bootstrap tab, doesn't lead to the correct tab

I've seen a few of related questions arround here, but I couldn't find a solution for me so far. 我在这里看到了一些相关的问题,但是到目前为止,我还没有找到解决方案。

Here's the thing: I have a tab section with the same structure as this and it's working just fine. 事情是这样的:我有一个tab部分,其结构与相同,并且工作正常。 But if I create an URL to, let's say, the second tab, like this www.example.com/path/to#tab2 it takes me to the default tab, tab1. 但是,如果我创建第二个选项卡的URL,例如www.example.com/path/to#tab2它将带我到默认选项卡tab1。

In my research, I read that something this could be a solution: 在我的研究中,我读到这可能是一种解决方案:

$(function(){
  var hash = window.location.hash;
  hash && $('ul.nav a[href="' + hash + '"]').tab('show');

  $('.nav-tabs a').click(function (e) {
    $(this).tab('show');
    var scrollmem = $('body').scrollTop();
    window.location.hash = this.hash;
    $('html,body').scrollTop(scrollmem);
  });
});

After that, if I go to www.example.com/path/to and navigate the tabs, they are working as before, however, if I go directly to this url www.example.com/path/to#tab2 , I get this error in the console: 之后,如果我转到www.example.com/path/to并浏览选项卡,它们将像以前一样工作,但是,如果我直接转到此网址www.example.com/path/to#tab2 ,则会得到控制台中的此错误:

Uncaught TypeError: $(...).tab is not a function

And as far as I know, that's a problem with Jquery loading, which makes no sense to me because my .js file already has jquery functions and they are working. 据我所知,这是Jquery加载的问题,对我来说这没有意义,因为我的.js文件已经具有jquery函数并且它们可以正常工作。

Jquery version: 1.12.4; jQuery版本:1.12.4;

Bootstrap version: 3.3.5; 引导程序版本:3.3.5;

Actually found the answer myself, this is what I did: 其实我自己找到了答案,这就是我所做的:

var hash = document.location.hash;
var prefix = '/';

if (hash) {
  $('.nav-tabs a[href="' + hash.replace(prefix, '') + '"]').tab('show');
}

I was close to make it work, just wasn't using the correct prefix before. 我已经接近使其工作了,只是之前没有使用正确的前缀。

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

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