使用URL打开jQuery选项卡

Mic*_*ael 0 jquery tabs

我有一个带有选项卡的页面,该页面是您的jQuery脚本

http://www.sohtanaka.com/web-design/simple-tabs-w-css-jquery/

我希望能够使选项卡3成为用户转到URL http://mysite.com/about.php#tab3时第一个打开的选项卡

那可能吗?

Via*_*kov 5

根据示例,您可以通过以下方式对其进行修改:

$(function() {
    $(".tab_content").hide(); //Hide all content
    //On Click Event (left standart)
    $("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 href attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active ID content
        return false;
    });


    // here we are looking for our tab header
    hash = window.location.hash;
    elements = $('a[href="' + hash + '"]');
    if (elements.length === 0) {
        $("ul.tabs li:first").addClass("active").show(); //Activate first tab
        $(".tab_content:first").show(); //Show first tab content
    } else {
        elements.click();
    }
});
Run Code Online (Sandbox Code Playgroud)

工作示例在这里。小心-哈希是硬编码的,因为我不知道如何将其传递给测试框架:(