1 回答
TA贡献1824条经验 获得超8个赞
问题是 $(this).attr('id') 只返回 id 而 window.location.hash 还包括“#”字符。
我简化了一点:
// I have to force a hash for the example here to work
location.hash = "it-services";
// As well as get the hash I remove "#" character. It will make things easier
var hash = window.location.hash.slice(1);
if (hash > "") {
$('.tab-button').removeClass('active');
$('.tab-box-content').removeClass('active-block');
$(`#${hash}`).addClass('active');
$(`.${hash}`).addClass('active-block');
}
.active, .active-block {
background: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="navigations-tab">
<div class="menu-tab">
//Menu Tab
<div class="tab-button active" id="technology">
<span>All Technology</span>
</div>
<div class="tab-button" id="datcom">
<span>Data Communication & Internet</span>
</div>
<div class="tab-button" id="it-services">
<span>IT Services</span>
</div>
<div class="tab-button" id="managed-services">
<span>Managed Services</span>
</div>
<div class="tab-button" id="smart-city">
<span>Smart City</span>
</div>
</div>
//Tab Content
<div class="tab-box-content technology active-block">
Tab for Tech
</div>
<div class="tab-box-content datcom">
Tab for Data Communication
</div>
<div class="tab-box-content it-services">
Tab for IT Services
</div>
<div class="tab-box-content managed-services">
Tab for Managed Services
</div>
<div class="tab-box-content smart-city">
Tab for Smart City
</div>
</div>
添加回答
举报