【发布时间】:2021-07-16 18:25:31
【问题描述】:
当我正常加载页面时,它会显示更正。但是,当尝试通过 url IE: page.php#tab2 加载选项卡时,选项卡导航不会选择我当前所在的选项卡,而是会加载默认选项卡“tab1”
jquery:
$(document).ready(function() {
//When page loads...
$(".tab_content").hide(); //Hide all content
$("ul.tabs li:first").addClass("active").show(); //Activate first tab
$(".tab_content:first").show(); //Show first tab content
var tabId = location.hash; // will look something like "#h-02"
if(tabId){
$(".tab_content").hide(); //Hide all content
$(tabId).addClass("active").show(); //Activate first tab
$(tabId).show(); // this will fired only when url get hash
}
//On Click Event
$("ul.tabs li").click(function() {
$("ul.tabs li").removeClass("active"); //Remove any "active" class
$(this).addClass("active"); //Add "active" class to selected tab
$(".tab_content").hide(); //Hide all tab content
var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
$(activeTab).fadeIn(); //Fade in the active ID content
return false;
});
html:
<div id="tab2" class="tab_content">
<article class="module width_quarter">
<header><h3>GeoLocation</h3></header>
<div class="module_content">
Content added here...
</article><!-- end of styles article -->
<div class="clear"></div>
</div></div><!-- end of #tab2 -->
它在做什么... https://i.stack.imgur.com/40Tc5.png
我想要它做什么...... https://i.stack.imgur.com/O8Fit.png
导航:
<section id="main" class="column">
<article class="module width_full">
<header><h3 class="tabs_involved">Lead ID: 993923</h3>
<ul class="tabs">
<li><a href="#tab1">Profile</a></li>
<li><a href="#tab2">Roof Information</a></li>
<li><a href="#tab3">Financials</a></li>
<li><a href="#tab4">Work Orders</a></li>
<li><a href="#tab5">Contracts</a></li>
<li><a href="#tab6">Documents</a></li>
<li><a href="#tab7">Gallery</a></li>
</ul>
</header>
【问题讨论】:
-
你能显示你的导航标记吗?这似乎是您要询问的领域
-
@Kinglish 添加:)
-
window.location.hash或者如果我没记错的话,这是一个纯 CSS 问题:target。否则你真的应该考虑探索developer.mozilla.org/en-US/docs/Web/API/History和developer.mozilla.org/en-US/docs/Web/API/History/pushState和developer.mozilla.org/en-US/docs/Web/API/History/replaceState -
您对导航和标签内容使用相同的 ID。这将是一个问题
标签: javascript php jquery tabs