简体   繁体   English

如何直接链接到特定的JavaScript选项卡?

[英]How can I link directly to a specific JavaScript tab?

My website has a tabbed interface that is used consistently throughout all of the pages. 我的网站有一个标签界面,在所有页面中使用一致。 I can link to the base page (the first tab), but can't figure out how to link to a specific tab. 我可以链接到基页(第一个选项卡),但无法弄清楚如何链接到特定选项卡。

This is my JavaScript: 这是我的JavaScript:

$(document).ready(function() {
  $(".tabs a").click(function() {
    var $this = $(this);
    $(".panel").hide();
    $(".tabs a.active").removeClass("active");
    $this.addClass("active").blur();
    var panel = $this.attr("href");
    $(panel).fadeIn(250);
    return false;
  });
$(".tabs li:first-of-type a").click();
});

And each of my pages is set up like this: 我的每个页面都设置如下:

<div id="aboutContainer">
    <ul class="tabs">
        <li><a href="#panel1">About Us</a></li>
        <li><a href="#panel2">Contact Us</a></li>
        <li><a href="#panel3">Mailing List</a></li>
        <li><a href="#panel4">Terms</a></li>
        <li><a href="#panel5">Privacy Policy</a></li>
        <li><a href="#panel6">Help</a></li>
    </ul>
        <div class="panelContainer">
            <div class="panel" id="panel1">
                <?php include('includes/aboutme.php'); ?>
            </div>

            <div class="panel" id="panel2">
                <?php include('includes/contact.php'); ?>
            </div>

            <div class="panel" id="panel3">
                <?php include('includes/mailinglist.php'); ?>
            </div>

            <div class="panel" id="panel4">
                <?php include('includes/terms.php'); ?>             
            </div>

            <div class="panel" id="panel5">
                <?php include('includes/privacypolicy.php'); ?>
            </div>

            <div class="panel" id="panel6">
                <?php include('includes/cheekyreference.php'); ?>
            </div>
        </div>
</div>

Any pointers would be greatly appreciated. 任何指针都将非常感激。 I'm pretty new to JavaScript and haven't been able to find an answer searching the web. 我是JavaScript的新手,并且无法在网上找到答案。

To clarify things a little: 澄清一点:

If I'm on the about.php page, the tabbed interface works. 如果我在about.php页面上,则选项卡式界面可以正常工作。 But the kind of link I'm trying to make is as follows: If I were on another page, I'd like to link to a particular tab on the about page. 但我想要的链接类型如下:如果我在另一个页面上,我想链接到about页面上的特定选项卡。 I have a navigation footer and I'd like to be able to click on a hyperlink such as 'mailing list' or 'contact' and have the correct page show up as well as the correct tab. 我有一个导航页脚,我希望能够点击“邮件列表”或“联系人”等超链接,并显示正确的页面以及正确的选项卡。

例如,如果您要专门单击“条款”选项卡,则可以执行以下操作:

$(".tabs a[href='#panel14']").click();

The technique to achieve this is to use the hash part of the url. 实现此目的的技术是使用url的哈希部分。 For example: 例如:

about.php#panel1 about.php#PANEL1

Or, similar, with a hashbang: 或者,类似的,与hashbang:

about.php#!panel1 about.php#!PANEL1

(The hashbang is a better choice if you are concerned with SEO.) (如果你关心SEO,那么hashbang是一个更好的选择。)

Then you need to include in your page a script that will detect the hash part of the url, and activate the tabs accordingly. 然后,您需要在页面中包含一个脚本,该脚本将检测URL的哈希部分,并相应地激活选项卡。

Sorry, I do this all the time but don't have a public example available. 对不起,我一直这样做,但没有公开的例子。 I'll update the post when I have one. 当我有一个帖子时,我会更新帖子。

In jQuery it would look like this: 在jQuery中它看起来像这样:

$(".tabs a[href="+window.location.hash+"]").click();

Note: you could also use a querystring, but a hash will prevent a page refresh if you are already on the page. 注意:您也可以使用查询字符串,但如果您已经在页面上,则哈希将阻止页面刷新。

$('a[href="#panel1"]').click(function() {
        //insert action here;
});​

Maybe this is overkill but you should be able to do it with client site routing. 也许这有点矫枉过正,但您应该可以通过客户端站点路由来实现。

You would first need to set up your "routes" that get activated based on the url in the address bar then you can have links in the following format link to tab1 on about and in the activated route activate the appropriate tab (fire the click event on that tab). 您首先需要设置根据地址栏中的URL激活的“路线”,然后您可以使用以下格式的链接连接到tab1 on about和激活的路径中激活相应的选项卡(触发click事件)在那个标签上)。

Here's a link to a jQuery plugin to provide JavaScript routing 这是一个提供JavaScript路由的jQuery插件的链接

http://jsrouter.codeplex.com/ http://jsrouter.codeplex.com/

There are of course other routing solutions, here's a link to a stackoverflow post on the subject javascript clientside routing/pathing library 当然还有其他路由解决方案,这里是关于主题javascript客户端路由/路径库的stackoverflow帖子的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM