jquery how to show specific tab with link from another page

随声附和 提交于 2020-01-03 05:59:13

问题


how to show specific tab with link from another page

<a href="index.php?page=home#tab2">Home</a>

this is the JS code:

$(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

    //On Click Event
    $("ul.tabs li").click(function() {

        $("ul.tabs li").removeClass("selected"); //Remove any "active" class
        $(this).addClass("selected"); //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;
    });


});

回答1:


Something like this maybe?

var openTab = $(location.hash).filter(".tab_content");

if(openTab.length){
  $("a[href='"+location.hash+"']").click();
}



回答2:


If you're looking at using the hash in a URL to pre-select a tab when the page loads, simply use window.location.hash to store an identifier of the current selected tab (element ID?), then read window.location.hash when the document ready event fires, and react to any element ID in there.



来源:https://stackoverflow.com/questions/2419302/jquery-how-to-show-specific-tab-with-link-from-another-page

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