最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Link straight into a specific tab-pane from another page - Stack Overflow

programmeradmin1浏览0评论

I am using the following code to separate content on a website and it is working fine.

However, if I wanted to link straight to a specific tab from another page how can I go about doing this?

Using the regular way with an ID in the url doesn't seem to work - e.g. mypage.html#a

Thanks in advance

I am using the following code to separate content on a website http://www.bootply./74926 and it is working fine.

However, if I wanted to link straight to a specific tab from another page how can I go about doing this?

Using the regular way with an ID in the url doesn't seem to work - e.g. mypage.html#a

Thanks in advance

Share Improve this question edited Aug 11, 2016 at 22:32 Tom el Safadi 6,8067 gold badges55 silver badges119 bronze badges asked Aug 11, 2016 at 22:18 tinOfBeanstinOfBeans 7371 gold badge9 silver badges22 bronze badges 1
  • Hey, did it work? If yes could you please accept my answer? – Tom el Safadi Commented Sep 1, 2016 at 18:06
Add a ment  | 

2 Answers 2

Reset to default 6

Although I do not have the chance to try it, I think you can do it via javascript.

When you are ming from another page to ex. mypage#abc, #abc can be retrieved by window.location.hash. So, a simple solution could be this javascript:

$(document).ready(function(){
    // get the tab from url
    var hash = window.location.hash;
    // if a hash is present (when you e to this page)
    if (hash !='') {
        // show the tab
        $('.nav-tabs a[href="' + hash + '"]').tab('show');
    }
});

Just make sure that the tabs you need to link (from other pages) have ids same as your hashes

The Advantage with this code is that you can navigate to those tabs from other sites as well not only within the same page.

$('#tab1 a').click(function (e) {
    e.preventDefault();
    $('a[href="' + $(this).attr('href') + '"]').tab('show');
})

You can see it working in my JSFiddle below:

http://jsfiddle/Anokrize/nP2Zk/317/

发布评论

评论列表(0)

  1. 暂无评论