【问题标题】:jQuery tabs: how to create a link to a specific tabjQuery 选项卡:如何创建指向特定选项卡的链接
【发布时间】:2015-11-23 04:54:29
【问题描述】:

您好,我已经找到了这个话题: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

你能帮帮我吗?谢谢

【问题讨论】:

  • 你知道我们不能点击那个链接,对吧?
  • 链接问题中的答案如何没有帮助?它在这里包含解决方案 - var activeTab = $(this).find("a").attr("href"); $(activeTab).show();
  • 在上面的代码中,您没有使用您引用的主题的“解决方案”......这可能会有所帮助。
  • 同时提供您的 html 代码,以便我们快速提供帮助
  • 我刚做完,先谢谢了。

标签: javascript php jquery url hyperlink


【解决方案1】:
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();
    }

});

【讨论】:

  • 哦,谢谢!这是工作,与我在链接中给出的解决方案有什么区别?
  • 将此新添加到您的代码中 var tabId = window.location.hash;控制台.log(tabId); if(tabId !== "") { $(".tabs").find('a[href='+tabId+']').click(); } 并更改了行,使其获得正确的元素 // On ajoute le hash dans l'url. window.location.hash = $(this).children().attr("href");
  • 哦,好的,谢谢,我被困了几个小时。祝你有美好的一天。
猜你喜欢
  • 1970-01-01
  • 2021-06-05
  • 1970-01-01
  • 2015-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-17
  • 1970-01-01
相关资源
最近更新 更多