简体   繁体   English

通过jQuery链接到特定选项卡

[英]link to a specific tab by jquery

I need your help! 我需要你的帮助!

I have a tab function by jquery in my site. 我的网站上有一个通过jQuery的选项卡功能。 and it works well. 而且效果很好。

Here is a javascript in < head > 这是<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>

and html sources are here! 和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>

But i need to link a button to a specific tab. 但是我需要将按钮链接到特定选项卡。 there is the button in a category archive page, and when users click the button, the page opens with the contents in the second tab, id named "type2". 在类别归档页面中有一个按钮,当用户单击该按钮时,该页面将打开,其中包含第二个选项卡中的内容,标识为“ type2”。

above sources are referred to by this page and here is the page that i'm working on. 以上资源均由此页面引用, 是我正在处理的页面

i'm waiting for your advices! 我正在等待您的建议!

Assuming your html file above is named index.html, you can create a link like: 假设上面的html文件名为index.html,则可以创建如下链接:

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

Then: 然后:

$(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