简体   繁体   English

Shopify 从另一个页面上的外部链接加载特定选项卡

[英]Shopify load specific tab from external link on another page

Hi there I am using these shopify tabs in a page, not product description, to get my design.您好,我在页面中使用这些 shopify 选项卡来获取我的设计,而不是产品描述。

Here is the link I received the tabs from - https://help.shopify.com/en/themes/customization/products/features/add-tabs-to-product-descriptions这是我收到的标签的链接 - https://help.shopify.com/en/themes/customization/products/features/add-tabs-to-product-descriptions

<div>
<ul class="tabs">
<li><a href="#tab-1">Info</a></li>
<li><a href="#tab-2">Shipping</a></li>
<li><a href="#tab-3">Returns</a></li>
</ul>
<div id="tab-1">
content etc.
<div id="tab-2">
content etc.
</div>
<div id="tab-3">
</div>
</div>


$(document).ready(function() {
$('ul.tabs').each(function(){
  var active, content, links = $(this).find('a');
  active = links.first().addClass('active');
  content = $(active.attr('href'));
  links.not(':first').each(function () {
    $($(this).attr('href')).hide();
  });
  $(this).find('a').click(function(e){
    active.removeClass('active');
    content.hide();
    active = $(this);
    content = $($(this).attr('href'));
    active.addClass('active');
    content.show();
    return false;
  });
});
});

From this code I am wondering how I can get to a specific tab that is not active from a link on a different page.从这段代码中,我想知道如何从不同页面上的链接访问未激活的特定选项卡。

If I create a link and have it as:如果我创建一个链接并将其设置为:

<a href="../pages/customPage#tab-3">link To tab</a> 

It doesn't load the 3rd tab.它不加载第三个选项卡。 Still loads the first one as default.仍然默认加载第一个。

I've had a look online and can't seem to get anywhere.我在网上看了看,似乎无法到达任何地方。 Is this possible with this code in the above link or will I need to look for something else?使用上面链接中的代码可以做到这一点,还是我需要寻找其他东西? My JavaScript is limited and tried to edit it myself from different tutorials online but can't get anywhere.我的 JavaScript 受到限制,并尝试自己从不同的在线教程中对其进行编辑,但无处可去。

I am also having trouble keeping on that active tab if the page refreshes.如果页面刷新,我也无法保持在该活动选项卡上。 If I am on #tab-3 and the page refresh I go back to #tab-1如果我在 #tab-3 并且页面刷新我 go 回到 #tab-1

Is it to do with this part of the code?它与这部分代码有关吗? I've tried changing it with my limited knowledge and don't get anywhere:我试过用我有限的知识改变它,但没有得到任何地方:

var active, content, links = $(this).find('a');
  active = links.first().addClass('active');
  content = $(active.attr('href'));
  links.not(':first').each(function () {
    $($(this).attr('href')).hide();
  });

Pasted code from comment:从评论粘贴代码:

        window.onload = function() {
$('ul.tabs).each(function(){
  var openedHash = new URL(window.location.href).hash;
links.first().removeClass('active');
content.hide();
active = $('a[href='+ openedHash + ']');
content = $($('a[href='+ openedHash + ']').attr('href'));
active.addClass('active');
content.show();
  });
  $(this).find('a').click(function(e){
active.removeClass('active');
content.hide();
active = $(this);
content = $($(this).attr('href'));
active.addClass('active');
content.show();
return false;
  });
    });

Thanks谢谢

For get active tab after site reload u can try use new URL(window.location.href).hash要在站点重新加载后获取活动选项卡,您可以尝试使用new URL(window.location.href).hash

May be some like this可能是这样的

var openedHash = new URL(window.location.href).hash;
links.first().removeClass('active');
content.hide();
active = $('a[href='+ openedHash + ']');
content = $($('a[href='+ openedHash + ']').attr('href'));
active.addClass('active');
content.show();

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

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