Access specific tab through URL

≯℡__Kan透↙ 提交于 2019-12-13 06:13:13

问题


I found a tab system in jQuery on the internet but the guy who made it (and authorized it's use) isn't available for contact anymore, so this is why I ask this question here.

This is the code of my javascript that manages the different tabs.

window.onload=function() {​



// get tab container
  var container = document.getElementById("tabContainer");
    if (document.location.hash.length) {
        $('.tabs ul li a[href^="' + document.location.hash + '"]').click();
    }
// set current tab
var navitem = container.querySelector(".tabs ul li");
//store which tab we are on
var ident = navitem.id.split("_")[1];
navitem.parentNode.setAttribute("data-current",ident);
//set current tab with class of activetabheader
navitem.setAttribute("class","tabActiveHeader");

//hide two tab contents we don't need
var pages = container.querySelectorAll(".tabpage");
for (var i = 1; i < pages.length; i++) {
  pages[i].style.display="none";
}

//this adds click event to tabs
var tabs = container.querySelectorAll(".tabs ul li");
for (var i = 0; i < tabs.length; i++) {
  tabs[i].onclick=displayPage;
}
}

// on click of one of tabs
function displayPage() {
  var current = this.parentNode.getAttribute("data-current");
  //remove class of activetabheader and hide old contents
  document.getElementById("tabHeader_" + current).removeAttribute("class");
  document.getElementById("tabpage_" + current).style.display="none";

  var ident = this.id.split("_")[1];
  //add class of activetabheader to new active tab and show contents
  this.setAttribute("class","tabActiveHeader");
  document.getElementById("tabpage_" + ident).style.display="block";
  this.parentNode.setAttribute("data-current",ident);
}

This is the HTML:

            <div class="tabs">
            <ul>
                <li id="tabHeader_1">Les listes</li>
                <li id="tabHeader_2">Les membres</li>
            </ul>
        </div>
<div class="tabscontent">
            <div class="tabpage" id="tabpage_1">
                <h2>Les listes</h2>
                <p>Pellentesque habitant morbi tristique senectus...</p>
            </div>
            <div class="tabpage" id="tabpage_2">
                </div>

By default, the javascript loads the FIRST tab (tabHeader_1, tabpage_1). What I'd like is if I put for example in the url example.com/page.php#tabpage_2, it loads automatically the second tab.

Thanks a lot for your help.


回答1:


document.location.hash returns the hash with the number sign (#hash).
You also can't use .tabs ul li a as the selector because there is no a tag inside of the li.

Try using:

var hash = document.location.hash.substr(1); // skip 1 character (the number sign)
$('.tabs ul li[id^="' + hash + '"]').click();

Also, this script isn't in jQuery. It only uses 1 jQuery function throughout the whole script. I would consider this more pure JavaScript than jQuery.



来源:https://stackoverflow.com/questions/17732655/access-specific-tab-through-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!