开发者

jquery how to show specific tab with link from another page

开发者 https://www.devze.com 2022-12-22 18:06 出处:网络
how to show specific tab with link from another page <a href=\"index.php?page=home#tab2\">Home</a>

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;
    }开发者_JAVA技巧);


});


Something like this maybe?

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

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号