【问题标题】:Shopify load specific tab from external link on another pageShopify 从另一个页面上的外部链接加载特定选项卡
【发布时间】:2019-09-29 13:39:04
【问题描述】:

您好,我在页面中使用这些 shopify 选项卡,而不是产品描述,来获取我的设计。

这是我收到的标签的链接 - 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;
  });
});
});

从这段代码中,我想知道如何通过不同页面上的链接访问未激活的特定选项卡。

如果我创建一个链接并将其设置为:

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

它不会加载第三个标签。仍然默认加载第一个。

我在网上查了一下,好像没找到。使用上面链接中的代码可以做到这一点,还是我需要寻找其他东西?我的 JavaScript 受到限制,并尝试通过在线不同的教程自己编辑它,但无法到达任何地方。

如果页面刷新,我也无法保持在该活动标签上。如果我在 #tab-3 并且页面刷新,我会返回 #tab-1

与这部分代码有关吗?我已经尝试以我有限的知识对其进行更改,但一无所获:

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();
  });

注释中的粘贴代码:

        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;
  });
    });

谢谢

【问题讨论】:

    标签: javascript jquery html tabs shopify


    【解决方案1】:

    要在网站重新加载后获取活动标签,您可以尝试使用new URL(window.location.href).hash

    可能是这样的

    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();
    

    【讨论】:

    • 在哪里添加此代码?当我替换“var active, content, links =”....等时,整个选项卡区域现在都不起作用了。
    • 点击&lt;a href="../pages/customPage#tab-3"&gt;link To tab&lt;/a&gt; 后浏览器的网址里有#tab-3
    • 它破坏了标签的工作,所以我无法尝试任何东西。就好像它停止了一切工作,我现在什至无法使用标签。
    • 它基本上是页面顶部示例中的代码。我从链接中使用。我没有编辑它。只是改了类名。这就是我正在使用的。
    • shopify 是付费的吗?而且我无法访问此工具
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    相关资源
    最近更新 更多