I am currently using Zurb Foundation Tabs for small project. The tabs work great but I am running into a wall when trying to url target to an specific tab. I searched and found a method for getting this achived but when modifying and applying it to my example it is not working. I am getting error: “unrecognized expression: :nth-child”. How can I properly url link to a specific tab in Foundation 5? DEMO
JS/JQuery for url target
<script>
window.navigateTab = function (event) {
var url = $(event.target).attr('href');
var target = url.split('#')[1];
if (target.substr(0, 5) == 'panel') {
var state = {
tab: target.substr(5)
};
selectTab("tabs_edit", state.tab); //<-- change the tab
history.pushState(state, null, url); //<-- change the url
event.preventDefault();
}
}
window.onpopstate = function (event) {
if (event.state != null) {
selectTab("tabs_edit", event.state.tab); //<-- change the tab
}
};
function selectTab(id, tab) {
// activate only the right tab:
var tab = $("#" + id + "> dl > dd:nth-child(" + tab + ")");
console.log(tab);
var a = $("a", tab);
target = $('#' + a.attr("href").split('#')[1]);
siblings = tab.siblings();
settings = tab.closest('[data-tab]').data('tab-init');
tab.addClass(settings.active_class);
siblings.removeClass(settings.active_class);
target.siblings().removeClass(settings.active_class).end().addClass(settings.active_class);
}
$('#menu a').on("click", navigateTab);
$(document).foundation();
</script>
HTML
//Target an specific tab:
<ul id="menu" class="dropdown">
<li><a href="#panel1a">Tab 1</a></li>
<li><a href="#panel2a">Tab 2</a></li>
<li><a href="#panel3a">Tab 3</a></li>
</ul>
//Actual Tabs
<dl id="tabs_edit" class="tabs vertical" data-tab>
<dd class="active"><a href="#panel1a" onclick="navigateTab(event)">Tab 1</a></dd>
<dd><a href="#panel2a" href="#panel2a" onclick="navigateTab(event)">Tab 2</a></dd>
<dd><a href="#panel3a" href="#panel3a" onclick="navigateTab(event)">Tab 3</a></dd>
<dd><a href="#panel4a" href="#panel4a" onclick="navigateTab(event)">Tab 4</a></dd>
</dl>
<div class="tabs-content vertical">
<div class="content active" id="panel1a">
<p>Panel 1 content goes here.</p>
</div>
<div class="content" id="panel2a">
<p>Panel 2 content goes here.</p>
</div>
<div class="content" id="panel3a">
<p>Panel 3 content goes here.</p>
</div>
<div class="content" id="panel4a">
<p>Panel 4 content goes here.</p>
</div>
</div>
I know this post is old but after reading more into the foundation 5 text they've added Deeplinking features that works on different pages
Just add this :
data-options="deep_linking:true"
Example:
<dl class="tabs vertical" data-tab data-options="deep_linking:true">
This even works on different pages linking to it!
For link on other pages I just called the panelid
Example:
<a href="product-line.php#panel12">
Deep linking is something that Foundation 5 has not included as found in Foundation 4. I have made a fix that is not as clever but gets the job done:
if(window.location.hash){
$('dl.tabs dd a').each(function(){
var hash = '#' + $(this).attr('href').split('#')[1];
if(hash == window.location.hash){
$(this).click();
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.