簡體   English   中英

通過jQuery鏈接到特定選項卡

[英]link to a specific tab by jquery

我需要你的幫助!

我的網站上有一個通過jQuery的選項卡功能。 而且效果很好。

這是<head>中的javascript

<script type="text/javascript">
// When the document loads do everything inside here ...
$(document).ready(function(){
    // When a link is clicked
    $("a.tab").click(function () {
    // switch all tabs off
    $(".active").removeClass("active");
    // switch this tab on
    $(this).addClass("active");
    // slide all content up
    $(".content").slideUp();
    // slide this content up
    var content_show = $(this).attr("title");
    $("#"+content_show).slideDown();
    });
});
</script>

和html資料在這里!

<ul id="tabs">
    <li><a href="#" title="type1" class="tab active">type1</a></li>
    <li><a href="#" title="type2" class="tab">type2</a></li>
</ul>
<section id="type1" class="content">
    <p>contents1contents1contents1contents1contents1</p>
</section>
<section id="type2" class="content content_2">
    <p>contents2contents2contents2contents2</p>
</section>

但是我需要將按鈕鏈接到特定選項卡。 在類別歸檔頁面中有一個按鈕,當用戶單擊該按鈕時,該頁面將打開,其中包含第二個選項卡中的內容,標識為“ type2”。

以上資源均由此頁面引用, 是我正在處理的頁面

我正在等待您的建議!

假設上面的html文件名為index.html,則可以創建如下鏈接:

<a href="index.html#!type2">click</a>

然后:

$(document).ready(function(){
    function doTab() {
        $(".active").removeClass("active");
        $(this).addClass("active");
        $(".content").slideUp();
        var content_show = $(this).attr("title");
        $("#"+content_show).slideDown();
    }

    // When a link is clicked
    $("a.tab").click(doTab);

    if (window.location.hash) {
        $('a[title="' + window.location.hash.substring(2) + '"]').click();
    }
});

暫無
暫無

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

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