簡體   English   中英

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

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM