简体   繁体   English

如何直接链接到网页内特定选项卡中的内容

[英]How to link directly to content in a specific tab within a webpage

I'm a technical writer using MadCap Flare to author an online output.我是一名技术作家,使用 MadCap Flare 创作在线输出。 Naturally, I'm pretty clueless when it comes to coding, but I have a beginner-level of knowledge of CSS and HTML5, with even slightly less knowledge of javascript.当然,我对编码一窍不通,但我对 CSS 和 HTML5 的了解只是初级水平,对 javascript 的了解就更少了。

Using online resources and articles, I managed to implement a mostly functional version of tabs within a webpage, but I'm struggling with figuring out how to link to a specific place within the tabs.使用在线资源和文章,我设法在网页中实现了一个主要功能版本的选项卡,但我正在努力弄清楚如何链接到选项卡中的特定位置。 Currently, the webpage will load the first tab by default, but I need to be able to link to a specific heading in the second or third tab.目前,网页默认会加载第一个选项卡,但我需要能够链接到第二个或第三个选项卡中的特定标题。

Here's my current javascript (only used to switch between tabs):这是我当前的javascript(仅用于在选项卡之间切换):

$(document).ready(function(){

    $('ul.tabs li').click(function(){
        var tab_id = $(this).attr('data-tab');

        $('ul.tabs li').removeClass('current');
        $('.tab-content').removeClass('current');

        $(this).addClass('current');
        $("#"+tab_id).addClass('current');
    })
})

I'm not sure how relevant, but here's the script that I have in my MasterPage.我不确定有多相关,但这是我的 MasterPage 中的脚本。

<script src="../Scripts/new_tabs.js" type="application/javascript">
    </script>

Here's my current html code:这是我当前的html代码:

    <ul class="tabs">
        <li class="tab-link current" data-tab="tab-1"><a name="Tab1">Tab 1 Label</a></li>
        <li class="tab-link" data-tab="tab-2"><a name="Tab2">Tab 2 Label</a></li>
        <li class="tab-link" data-tab="tab-3"><a name="Tab3">Tab 3 Label</a></li>
    </ul>
    <div class="tab-content current" id="tab-1">
        <p>Tab 1 content goes here</p>
    </div>
    <div class="tab-content" id="tab-2">
        <p>Tab 2 content goes here</p>
    </div>
    <div class="tab-content" id="tab-3">
        <p>Tab 3 content goes here</p>
    </div>

At the very least, I need the ability to link to a specific tab from other pages in the site, but ideally I'd be able to link to specific content within tabs, such as a Head1 or ap tag.至少,我需要能够从站点中的其他页面链接到特定选项卡,但理想情况下,我能够链接到选项卡中的特定内容,例如 Head1 或 ap 标记。

Can anyone assist a struggling non-coder?任何人都可以帮助苦苦挣扎的非编码员吗?

From what I understand, you are using JS to manage which tab and which contents are current.据我了解,您正在使用 JS 来管理哪个选项卡和哪些内容是最新的。 Since you are not putting each content on its own page and are making a single-page site, there is only one way I can think that this would work.由于您没有将每个内容都放在自己的页面上并且正在制作单页网站,因此我认为只有一种方法可以工作。

You can use search parameters to send specific data back and forth between your other pages.您可以使用搜索参数在其他页面之间来回发送特定数据。 For example:例如:

http://yourwebsite.com/?key=value http://yourwebsite.com/?key=value

Using this, you can send key/value pairs of data to other pages.使用它,您可以将数据的键/值对发送到其他页面。 Your <a> tag will look something like this on your other pages:您的<a>标记在您的其他页面上将如下所示:

<a href="http://yourwebsite.com/?tab=2">Link to Tab 2</a> <a href="http://yourwebsite.com/?tab=2">链接到选项卡 2</a>

You can get the data on your main page by doing something like:您可以通过执行以下操作获取主页上的数据:

$(document).ready(function(){
  let params = new URLSearchParams(location.search);
  let currentTab = params.get('tab') || 1;
  $('ul.tabs li').removeClass('current');
  $('.tab-content').removeClass('current');
  $('li[data-tab=tab-' + currentTab + ']').addClass('current');
  $("#tab-"+currentTab).addClass('current');

  $('ul.tabs li').click(function(){
      var tab_id = $(this).attr('data-tab');

      $('ul.tabs li').removeClass('current');
      $('.tab-content').removeClass('current');

      $(this).addClass('current');
      $("#"+tab_id).addClass('current');
      
  });
});

Let's go over the added code.让我们回顾一下添加的代码。 First, we have:首先,我们有:

let params = new URLSearchParams(location.search);

This is pretty straightforward.这很简单。 location.search will return our search parameters as a string. location.search将我们的搜索参数作为字符串返回。 In our case, it could be "?tab=2" .在我们的例子中,它可能是"?tab=2" Then we use the new URLSearchParams() web API to turn the string we got from location.search into an object so we can access/modify its data easier.然后我们使用新的 URLSearchParams() Web API 将我们从location.search获得的字符串转换为一个对象,以便我们可以更轻松地访问/修改其数据。 The resulting object is assigned to the variable params .结果对象被分配给变量params

Then we have:然后我们有:

let currentTab = params.get('tab') || 1;

This will assign currentTab with the value of the search parameter "tab" or the number 1 in case params.get('tab') is not defined (the number 1 is the default tab you want the users to see if there is no search parameter provided, ie they went to your page without clicking on the links on your other pages).这将为currentTab分配搜索参数“tab”的值或数字 1,以防params.get('tab')未定义(数字 1 是您希望用户查看是否没有搜索的默认选项卡提供的参数,即他们没有点击您其他页面上的链接就进入了您的页面)。

Lastly, we have:最后,我们有:

$('ul.tabs li').removeClass('current');
$('.tab-content').removeClass('current');
$('li[data-tab=tab-' + currentTab + ']').addClass('current');
$("#tab-"+currentTab).addClass('current');

This removes all current classes from all the list elements and tab-content divs.这将从所有列表元素和选项卡内容 div 中删除所有当前类。 It will then find the tab and the tab-content div based on our currentTab value we got earlier.然后它将根据我们之前获得的currentTab值找到选项卡和选项卡内容 div。

This way, if your users goes to:这样,如果您的用户去:

http://yourwebsite.com/?tab=2 http://yourwebsite.com/?tab=2

then the 2nd tab and its associated div should be given the current class.那么第二个选项卡及其关联的 div 应该被赋予当前类。

I hope this helps.我希望这有帮助。

~A 〜一个

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

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