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


jQuery tabs: how to create a link to a specific tab

你好,我已经找到了这个主题:jQuery选项卡:如何创建一个链接到特定的选项卡?

但这并不能解决我的问题,

这里是我的代码:

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

你能帮我吗?由于

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();
    }
});