简体   繁体   English

使用JQuery中的定位标记从另一个页面中选择特定标签

[英]Select a particular tab from another page using anchor tag in JQuery

Trying to link dropdown menu subpages to selected tab on another page. 尝试将下拉菜单子页面链接到另一页上的选定选项卡。 Dropdown menu here: http://arreolatran.com/clients/new_horizons/ , trying to link to selected sidenav tabs here: http://arreolatran.com/clients/new_horizons/about-us.html . 此处的下拉菜单: http ://arreolatran.com/clients/new_horizo​​ns/,尝试在此处链接至选定的sidenav选项卡: http ://arreolatran.com/clients/new_horizo​​ns/about-us.html。 For example, go to About Us dropdown menu > select Our Expertise (subpage) - link does not work. 例如,转到关于我们下拉菜单>选择我们的专业知识(子页面)-链接不起作用。 It goes to the correct page, but does not select the correct tab. 它将转到正确的页面,但没有选择正确的选项卡。 Dropdown menu nav code: 下拉菜单导航代码:

<div id="nav">
    <a href="index.html"><div class="nhImgs logo"></div></a>
    <ul id="pages">
        <li>
            <a href="about-us.html">About Us<div class="nhImgs caret"></div></a>
            <ul>
                <li><a href="about-us.html#ourExpertise">Our Expertise</a></li>
                <li><a href="about-us.html#whyChooseUs">Why Choose Us?</a></li>
                <li><a href="about-us.html#tammy">Dr. Tammy Mondry</a></li>
                <li><a href="about-us.html#joe">Dr. Joe Mondry</a></li>
            </ul>
        </li>
        <li>
            <a href="lymphedema.html">Lymphedema<div class="nhImgs caret"></div></a>
            <ul>
                <li><a href="lymphedema.html#idLymph">Identifying Lymphedema</a></li>
                <li><a href="lymphedema.html#examplesLymph">Examples of Lymphedema</a></li>
                <li><a href="lymphedema.html#lymphTreat">Lymphedema Treatment</a></li>
                <li><a href="lymphedema.html#lymphFaqs">Lymphedema FAQs</a></li>
                <li><a href="lymphedema.html#lymphProd">Products</a></li>
            </ul>
        </li>
        <li>
            <a href="cancer-rehabilitation.html">Cancer Rehabilitation<div class="nhImgs caret"></div></a>
            <ul>
                <li><a href="cancer-rehabilitation.html#benefitsExe">Benefits of Exercise</a></li>
                <li><a href="cancer-rehabilitation.html#assessTest">Assessment and Testing</a></li>
                <li><a href="cancer-rehabilitation.html#programSpec">Program Specifics</a></li>
                <li><a href="cancer-rehabilitation.html#cancerExeFaqs">Cancer &amp; Exercise FAQs</a></li>
            </ul>
        </li>
        <li>
            <a href="patient-guide.html">Patient Guide<div class="nhImgs caret"></div></a>
            <ul>
                <li><a href="patient-guide.html#publications">Publications</a></li>
                <li><a href="patient-guide.html#downloadsForms">Downloads/Forms</a></li>
                <li><a href="patient-guide.html#relatedInfo">Related Information</a></li>
            </ul>
        </li>
        <li>
            <a href="contact-us.html">Contact Us</a>
        </li>
    </ul>
</div>

Sidenav tab code: Sidenav标签代码:

<div class="sideNavCont">
    <div id="sidenav">
        <ul class="colL">
          <li class="sidenavHdrText"><a href="#about1">About Us</a></li>
          <li><a href="#expertise2">Our Expertise</a></li>
          <li><a href="#why3">Why Choose Us?</a></li>
          <li><a href="#tammy4">Dr. Tammy Mondry</a></li>
          <li><a href="#joe5">Dr. Joe Mondry</a></li>
        </ul>

        <div id="about1" class="sideNavColR">
          <ul>
            <h1 id="aboutUs" class="pageTtl">About Us</h1>
              <p>Content here.</p>
        </div>

        <div id="expertise2" class="sideNavColR">
          <ul>
                <h1 id="ourExpertise">Our Expertise</h1>
              <p>Content here.</p>
          </ul>    
        </div>

        <div id="why3" class="sideNavColR">
          <ul>
              <h1 id="whyChooseUs">Why Choose Us</h1>
              <p>Content here.</p>
            </ul>    
        </div>

        <div id="tammy4" class="sideNavColR">
          <ul>
              <h1 id="tammy">Dr. Tammy Mondry</h1>
              <p>Content here.</p>
          </ul>    
        </div>

        <div id="joe5" class="sideNavColR">
          <ul>
              <h1 id="joe">Dr. Joe Mondry</h1>
              <p>Content here.</p>
          </ul>    
        </div>
    </div>
</div>

JS for sidenav tabs: sidenav标签的JS:

// JavaScript Document
$(function () {
    $('#sidenav div').hide();
    $('#sidenav div:first').show();
    $('#sidenav ul li:first').addClass('active');
    $('#sidenav ul li a').click(function () {
        var currentTab = $(this).attr('href');
        var vis = $(currentTab).is(':visible');
        $('#sidenav div').hide();
        $('#sidenav ul li').removeClass('active');
        $(this).parent().addClass('active');
        if (vis) {
            $(currentTab).hide();
        } else {
            $(currentTab).show();
        }
    });
});

I think you were close, but there were a couple of things that need to be modified. 我认为您很亲密,但是有几件事需要修改。 Make a backup of your current files before trying any of the following. 尝试以下任何操作之前,请备份当前文件。

Try the following JS for your sidebar navigation: 尝试使用以下JS进行侧边栏导航:

// JavaScript Document
$(document).ready(function() {
    $('#sidenav div').hide();
    $('#sidenav div:first').show();
    $('#sidenav ul li:first').addClass('active');

    function handleNavSwitch(linkObj) {
    var currentTab = $(linkObj).attr('href');
    if (currentTab.substr(0, 1) != '#') return;

        var vis = $(currentTab).is(':visible');
    if (!vis) {
            $('#sidenav div').hide();
            $('#sidenav ul li').removeClass('active');
            //$(linkObj).parent().addClass('active');
        var hashTag = currentTab.substr(1);
        $('#sidenav ul li a[href="#'+hashTag + '"]').parent().addClass('active');
        $(currentTab).show();
        }  
    }

    $('#nav ul li a, #sidenav ul li a').click(function() { handleNavSwitch(this); });
    // Check if hash exists
    if(window.location.hash) {
    // Retrieve hash value (not including the "#")
    var hashTag = window.location.hash.substr(1);
    // Make sure that there's an actual value for the hash
    if (hashTag.length > 0) {
        // Find the link with the matching 
        var $link = $('#sidenav ul li a[href="#'+hashTag + '"]');

        //if ($link.length > 0) handleNavSwitch( $link.get(0) );
        if ($link.length > 0) $link.trigger('click');
    }
    }

});

The above changes take into account that you need to be able to: 上述更改考虑到您需要具备以下能力:

  1. Navigate to the content sections (ie our expertise, why choose us, etc.) from both the top/main navigation, as well as from the side navigation. 从顶部/主导航以及侧面导航中导航至内容部分(即,我们的专业知识,为什么选择我们等)。
  2. Take the hash part of the link into consideration, eg #ourExpertise, #whyChooseUs. 考虑链接的哈希部分,例如#ourExpertise,#whyChooseUs。

You'll also need to address some consistency issues in your links. 您还需要解决链接中的一些一致性问题。 For example, in your about us links you use #ourExpertise, #whyChooseUs, #tammy, and #joe. 例如,在关于我们的链接中,您使用#ourExpertise,#whyChooseUs,#tammy和#joe。 Then in your sidebar code you have #expertise2, #why3, #tammy4, and #joe5. 然后,在侧边栏代码中,您将拥有#expertise2,#why3,#tammy4和#joe5。 These names should both be the same... so change references to #expertise2 to #ourExpertise, etc. 这些名称应相同...因此将对#expertise2的引用更改为#ourExpertise等。

Finally, because of the way you have implemented this, you will probably also have to adjust the header menu links on each of your respective pages. 最后,由于实现方法的不同,您可能还必须调整各个页面上的标题菜单链接。 For example, in the provided example you have "about-us.html#ourExpertise" - however, for the above changes to work you need to remove "about-us.html" from the URL so that it is just "#ourExpertise"... this ONLY applies to the about-us.html page. 例如,在提供的示例中,您具有“ about-us.html#ourExpertise”-但是,要使上述更改生效,您需要从URL中删除“ about-us.html”,以便它只是“ #ourExpertise” ...这仅适用于about-us.html页面。 On the lymphedema.html page you'd need to remove lymphedema.html from the menu links, etc. so that you would have #idLymph instead of lymphedema.html#idLymph. 在Lymphedema.html页面上,您需要从菜单链接等中删除lymphedema.html,以便使用#idLymph而不是lymphedema.html#idLymph。

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

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