jQuery选项卡:如何创建指向特定选项卡的链接

时间:2015-08-28 13:44:37

标签: javascript php jquery url hyperlink

您好我已经找到了这个主题:jQuery tabs: how to create a link to a specific tab?

但那不能解决我的问题,

这里是我的代码:

$(document).ready(function() {

//Default Action
$(".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("active"); //Remove any "active" class
    $(this).addClass("active"); //Add "active" class to selected tab
    $(".tab_content").hide(); //Hide all tab content
    var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
    $(activeTab).fadeIn(); //Fade in the active content
    e.preventDefault();         
    yInitPos = $(window).scrollTop();       
    // On ajoute le hash dans l'url.
    window.location.hash = $(this).attr("href");

    return false;
});
});

我无法访问带有以下链接的标签:http://127.0.0.1/admin.php#tab2

你能帮帮我吗?感谢

1 个答案:

答案 0 :(得分:0)

Try this below script, just made once change to the // on ajoute le hash dans

and added some code below the click function

$(document).ready(function() {

    //Default Action
    $(".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(e) {
        $("ul.tabs li").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content
        var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active content
        e.preventDefault();         
        yInitPos = $(window).scrollTop();       
        // On ajoute le hash dans l'url. 
// $(this) below will be <li> tag, so you have to get to the children <a> tag with href attr
        window.location.hash = $(this).children().attr("href");

        return false;
    });
// the code below gets the hash(#) value from the url and finds the link with a href of this # value and triggers it
    var tabId = window.location.hash;
    console.log(tabId);
    if(tabId !== "")
    {
        $(".tabs").find('a[href='+tabId+']').click();
    }

});