jquery如何显示来自另一个页面的链接的特定选项卡

时间:2010-03-10 18:01:38

标签: jquery tabs

如何显示包含其他网页链接的特定标签

<a href="index.php?page=home#tab2">Home</a>

这是JS代码:

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


});

2 个答案:

答案 0 :(得分:2)

这样的事可能吗?

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

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

答案 1 :(得分:1)

如果您正在查看在页面加载时使用URL中的哈希来预先选择选项卡,只需使用window.location.hash存储当前所选选项卡的标识符(元素ID?),然后阅读当文档就绪事件触发时,window.location.hash会对其中的任何元素ID做出反应。