选项卡中的a href链接没有执行任何操作

a href links within tabs not doing anything

本文关键字:执行 任何 操作 链接 href 选项      更新时间:2023-09-26

我在引导选项卡中使用以下代码,以便在url中显示选项卡的#名称,因为我希望在完成一些后期操作后重定向到特定的选项卡,这很好。

然而,自从我开始使用该代码以来,所有其他链接都停止了任何操作,它不会打开页面或显示错误,什么都没有。

这是我的代码:

$(function() {
var gotoHashTab = function (customHash) {
    var hash = customHash || location.hash;
    var hashPieces = hash.split('?'),
    activeTab = $('[href=' + hashPieces[0] + ']');
    activeTab && activeTab.tab('show');
}
// onready go to the tab requested in the page hash
gotoHashTab();
// when the nav item is selected update the page hash
$('.nav a').on('shown.bs.tab', function (e) {
    window.location.hash = e.target.hash
    //location.hash = $(e.target).attr('href').substr(1);
    scrollTo(0,0);      
})
// when a link within a tab is clicked, go to the tab requested
$('.tab-pane a').click(function (event) {
    if (event.target.hash) {
        e.preventDefault();
        gotoHashTab(event.target.hash);
    }
})
$('.tab-pane a').on('click', function(e){
    e.preventDefault() 
})

});

我注意到,在其他一些问题上,e.preventDefault()可能会导致这种情况,但当我评论这些问题时,什么也没发生。

如何重写代码以保持功能,但使那些正常的链接重新工作?

它正在查看带有以下内容的所有链接:

  activeTab = $('[href=' + hashPieces[0] + ']');

更改为:

activeTab = $('.tab-pane [href=' + hashPieces[0] + ']');

也可以尝试更具体地使用.nav,因为.nav是所有导航的默认值。所以这个:

$('.nav a').on('shown.bs.tab', function (e) {

应更改为:

 $('.tab-pane a').on('shown.bs.tab', function (e) {