Twitter Bootstrap 选项卡:转到页面重新加载或超链接上的特定选项卡

问题描述:

我正在开发一个网页,我在其中使用 Twitter 的 Bootstrap 框架及其 引导标签 JS.除了一些小问题外,它的效果很好,其中之一是我不知道如何从外部链接直接转到特定选项卡.例如:

I'm developing a web page in which I'm using Twitter's Bootstrap Framework and their Bootstrap Tabs JS. It works great except for a few minor issues, one of which is I do not know how go directly to a specific tab from an external link. For example:

<a href="facility.php#home">Home</a>
<a href="facility.php#notes">Notes</a>

应该分别转到主页"选项卡和备注"选项卡点击外部页面的链接时

should go to the Home tab and the Notes tab respectively when clicked on the links from an external page

这是我对问题的解决方案,可能有点晚了.但它也许可以帮助其他人:

Here is my solution to the problem, a bit late perhaps. But it could maybe help others:

// Javascript to enable link to tab
var hash = location.hash.replace(/^#/, '');  // ^ means starting, meaning only match the first hash
if (hash) {
    $('.nav-tabs a[href="#' + hash + '"]').tab('show');
} 

// Change hash for page-reload
$('.nav-tabs a').on('shown.bs.tab', function (e) {
    window.location.hash = e.target.hash;
})