如何因 url 中的主题标签而停用另一个活动标签?

How to deactivate an active tab for another because of a hashtag in the url?

我有一个已激活的标签,但如果主题标签指向另一个标签,我想为另一个标签停用该标签。

这是我的代码:

HTML:

<ul class="nav">
    <li class="active">
        <a class="default-tab" href="#default-tab">About</a>
    </li>


    <li>
        <a class="brand-tab" href="#brand-tab">Brand</a>
    </li>
</ul>


<div class="tab-content">
    <div class="default-tab active" id="default-tab">
        <p>...</p>
    </div>

    <div class="brand-tab" id="boc-brand">
        <p>...</p>
    </div>
</div>

JS:

$(document).ready(function() {
    if (location.hash === "#brand", function() {
            $(".brand-tab").addClass('active', function() {
                $(".default-tab").removeClass('active');
            });
        });
});

你有不属于它们的回调。这是您需要的代码:

$(document).ready(function() {
    if (location.hash === "#brand") {
        $(".brand-tab").addClass('active');
        $(".default-tab").removeClass('active');
    }
});

编辑:

如果这些是 Bootstrap 标签,正如该标记所显示的那样,您最好使用内置的 Bootstrap 方法:

$(document).ready(function() {
    if (location.hash === "#brand") {
        $('a[href="#brand-tab"]').tab('show');
    }
});