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

时间:2010-08-30 14:01:50

标签: javascript jquery tabs

我正在为标签使用简单的jQuery脚本:

JS:

$(document).ready(function() {

    $(".tab-content").hide();
    $("ul.tabs li:first").addClass("active").show();
    $(".tab-content:first").show();

    $("ul.tabs li").click(function() {
        $("ul.tabs li").removeClass("active");
        $(this).addClass("active");
        $(".tab-content").hide();
        var activeTab = $(this).find("a").attr("href");
        $(activeTab).show();
        return false;
    });

});

HTML(对于index.html):

<div id="tabs">

    <ul class="tabs">
        <li><a href="#tabs-voters">Top Voters</a></li>
        <li><a href="#tabs-commenters">Top Commenters</a></li>
    </ul>

    <div id="tabs-voters" class="tab-content">

    <p id="h1-01">Tab content</p>

        <p>Some content</p>

        </div>

    <div id="tabs-commenters" class="tab-content">

        <h2 id="h-02">Tab content</h2>

        <p>Some content</p>

        <h2 id="h-03">Tab content</h2>

        <p>Some content</p>

        </div>

</div>

我需要做的是创建 index.html#h-02 index.html#h-03 等的工作链接,但这些简单链接不起作用,因为默认情况下隐藏选项卡。是否可以修改JS,因此我可以链接到打开index.html时隐藏的选项卡中的书签?有人能指出我正确的方向吗?

非常感谢! :)

7 个答案:

答案 0 :(得分:9)

在文档就绪处理程序中,您可以检查片段的值并使用JavaScript显示相应的选项卡。

$(document).ready(function () {
    ...

    var tabId = window.location.hash; // will look something like "#h-02"
    $(tabId).click(); // after you've bound your click listener
});

根据要求修改

$(document).ready(function() {

    $(".tab-content").hide();
    $("ul.tabs li:first").addClass("active").show();
    $(".tab-content:first").show();

    $("ul.tabs li").click(function() {
        $("ul.tabs li").removeClass("active");
        $(this).addClass("active");
        $(".tab-content").hide();
        var activeTab = $(this).find("a").attr("href");
        $(activeTab).show();
        return false;
    });

    $(window.location.hash).click(); // super-simple, no? :)
});​

编辑2:

如果您希望能够指定标签内容元素的ID(如您链接的页面中的h-02),则必须向后工作以获取相应标签的ID以激活它。像这样:

$(document).ready(function() {
    var $tabContent = $(".tab-content"),
        $tabs = $("ul.tabs li"),
        tabId;

    $tabContent.hide();
    $("ul.tabs li:first").addClass("active").show();
    $tabContent.first().show();

    $tabs.click(function() {
        var $this = $(this);
        $tabs.removeClass("active");
        $this.addClass("active");
        $tabContent.hide();
        var activeTab = $this.find("a").attr("href");
        $(activeTab).show();
        return false;
    });

    // Grab the ID of the .tab-content that the hash is referring to
    tabId = $(window.location.hash).closest('.tab-content').attr('id');

    // Find the anchor element to "click", and click it
    $tabs.find('a[href=#' + tabId + ']').click();
});​

使用$.closest()表示哈希可以指定.tab-content本身的ID(例如示例中的tabs-commenters)或其子代。

我上面已经提出了一些其他的清理建议。无需继续重新选择那些DOM元素!

答案 1 :(得分:1)

似乎没有什么对我有用:

//Default Action
jQuery(".tab_content").hide(); //Hide all content
jQuery("ul.tabs li:first").addClass("active").show(); //Activate first tab
jQuery(".tab_content:first").show(); //Show first tab content

//On Click Event
jQuery("ul.tabs li").click(function() {
    jQuery("ul.tabs li").removeClass("active"); //Remove any "active" class
    jQuery(this).addClass("active"); //Add "active" class to selected tab
    jQuery(".tab_content").hide(); //Hide all tab content
    var activeTab = jQuery(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
    jQuery(activeTab).fadeIn(); //Fade in the active content
    return false;

});

答案 2 :(得分:0)

好吧,您可以将您的网址设为http://site.com/index.html?voters,然后在页面中检查window.location.search(搜索包含问号的位)。如果是"?voters",则运行代码以选择选项卡。如果是"?commenters",则运行代码以选择该选项卡。等等。

答案 3 :(得分:0)

您可以通过GET将参数传递到网页。

即。 www.yourpage.com?tab=tab1

然后例如(使用php)

<?php
$tab = $_REQUEST['tab'];
?>

然后在您的JS代码中,您可以执行以下操作:

var default_id = <?php echo $tab; ?>
$("#"+default_id).addClass("active").show();

虽然你应该检查以确保default_id有效,如果没有加载工作默认值(就像你已经做的那样)

编辑我刚刚注意到这个问题是想要使用格式化的网址,例如www.example.com#h-02,你最好只坚持使用JS

答案 4 :(得分:0)

我会尝试稍微修改你的代码。我就是这样做的:

http://jsfiddle.net/RtCnU/8/

这样你就能完全达到你想要的效果。

这是我使用的代码:

<div id="tabs">

    <ul class="tabs">
        <li><a href="#tabs-voters">Top Voters</a></li>
        <li><a href="#tabs-commenters">Top Commenters</a></li>
    </ul>

    <div id="wrap">

    <div id="tabs-voters" class="tab-content">

    <p id="h1-01">Tab content</p>

        <p>Some content</p>

        </div>

    <div id="tabs-commenters" class="tab-content">

        <h2 id="h-02">Tab cdsfdfontent</h2>

        <p>Some contesdfdsfsdfant</p>

        </div>
</div>

</div>

这是Javascript:

$(document).ready(function() {

    $(".tab-content").hide();
    $("ul.tabs li a:first").addClass("active").show();
    $("#wrap > div").hide().filter(":first").show();

    $("ul.tabs li a").click(function() {
        $("ul.tabs li > a").removeClass("active");
        $(this).addClass("active");
        $("#wrap > div").hide();
        var activeTab = $(this).attr("href");
        $(activeTab).show();
        return false;
    });

});

让我知道它是如何运作的!

答案 5 :(得分:0)

这是我根据马特的回答让我的工作。我发布这个来帮助别人。

<script>
$(document).ready(function(){
    $.tabs('#tabs a');
    $('#tabs a[tab=\'' + window.location.hash + '\']').trigger('click');
});
</script>

<div id="tabs" class="htabs">
    <a tab="#tab_general">General</a>
    <a tab="#tab_other">Other</a>
</div>
<div id="tab_general">
    Tab 1 here
</div>
<div id="tab_other">
    Tab 2 here
</div>

答案 6 :(得分:0)

我喜欢Dan Powers的答案,我目前正在使用它的版本。但是,我一开始认为它不起作用所以我做了alert(window.location.hash)并发现它包括#。 Dan的回答是有效的,因为他在他的标签ID中使用#,例如tab="#tab_general"。所以,要注意这一点。

如果您想要在没有#的情况下读取您的哈希名称,例如tab="tab_general",替换:

$('#tabs a[tab=\'' + window.location.hash + '\']').trigger('click');

$('#tabs a[tab=\'' + window.location.hash.substring(1) + '\']').trigger('click');

您当然也可以将tab更改为id或其他内容。

我还没弄清楚如何链接到嵌套标签,例如一个选项卡包含带有子选项卡的页面。

相关问题