我正在使用以下代码来分离网站上的内容http://www.bootply.com/74926,它运行良好。
但是,如果我想从另一个页面直接链接到特定选项卡,我该怎么做?
使用常规方式在URL中添加ID似乎不起作用-例如mypage.html#a
提前感谢。
但是,如果我想从另一个页面直接链接到特定选项卡,我该怎么做?
使用常规方式在URL中添加ID似乎不起作用-例如mypage.html#a
提前感谢。
mypage#abc
时,可以通过 window.location.hash
检索到 #abc
。因此,一个简单的解决方案可以是以下JavaScript代码:$(document).ready(function(){
// get the tab from url
var hash = window.location.hash;
// if a hash is present (when you come to this page)
if (hash !='') {
// show the tab
$('.nav-tabs a[href="' + hash + '"]').tab('show');
}
});
请确保您需要链接的选项卡(来自其他页面)与您的哈希值相同。
$('#tab1 a').click(function (e) {
e.preventDefault();
$('a[href="' + $(this).attr('href') + '"]').tab('show');
})
您可以在我的 JSFiddle 中看到它的工作: