Show tab with url

I wrote code that would change the li class to active and show the correct content when clicked, but I would also like to link the visitor, for example: www.socal.nu/index.php#tab2 to activate tab2.

the code:

$(document).ready(function() {

//Default Action
$(".tab_content").hide();
$("ul.tabs li:first").addClass("active").show();
$(".tab_content:first").show();

//On Click Event
$("ul.tabs li").click(function() {
$("ul.tabs li").removeClass("active");
$(this).addClass("active");
$(".tab_content").hide();
var activeTab = $(this).find("a").attr("href");
$(activeTab).fadeIn();
return false;
});

});

Edited to include the structure (x) of the html tab list (as published by OP in the comment on @slightlymore answer ):

<ul class="tabs">
  <li><a href="#tab1">Hem</a></li>
  <li><a href="#tab2">Projekt</a></li>
  <li><a href="#tab3">Om SOCAL</a></li>
  <li><a href="#tab4">Kontakt</a></li>
</ul>
+3
source share
2 answers

To get a tab from a URL, do the following:

var tab = window.location.hash;

Then you can trigger the click event for liwhich has athe correct one href:

var tab = window.location.hash;

    // Fire click on the <li> that has the <a> with the proper 'href' attribute
$("ul.tabs li:has(a[href=" + tab + "])").click();

Or you can reuse the function created for the click event and load the start page.

  // Function that is used for click event and page load
function loadTab() {
    $("ul.tabs li").removeClass("active");
    $(this).addClass("active");
    $(".tab_content").hide();
    var activeTab = $(this).find("a").attr("href");
    $(activeTab).fadeIn();
    return false;
}

  // set up Click Event
$("ul.tabs li").click( loadTab );

var tab = window.location.hash;

  // check to see if there was a tab in the location, and if
  //    so, call loadTab if from the context of the proper <li>.
if( $("ul.tabs li:has(a[href=" + tab + "])").length ) {
    loadTab.call($("ul.tabs li:has(a[href=" + tab + "])")[0]);
}
+1

, "url", , jQuery, . HTML:

<ul class="tabs">
  <li id="tab-1">Tab number 1</li>
  <li id="tab-2">Tab number 2</li>
  <li id="tab-3">Tab number 3</li>
</ul>

jQuery :

$(document).ready(function() {

    //Default Action
    $(".tab_content").hide();
    $("ul.tabs li:first").addClass("active").show();
    $(".tab_content:first").show();

    //On Click Event
    $("ul.tabs li").click(function() {
        $("ul.tabs li").removeClass("active");
        $(this).addClass("active");
        $(".tab_content").hide();
        var activeTab = $(this).find("a").attr("href");
        $(activeTab).fadeIn();
        return false;
    });

    // 'click' the tab with ID indicated in the URL
    $('ul.tabs li' + location.hash).trigger('click');
});

ALTERNATIVELY - LI, , , :

    //chop off the #tab bit from the URL, keeping the number at the end
    var tab = location.hash.substring(4);
    // 'click' the tab indicated in the URL
    $('ul.tabs li:nth-of-type(' + tab + ')').trigger('click');
0

Source: https://habr.com/ru/post/1752853/


All Articles