【问题标题】:Container redirect to URL but nested anchor go to different URL than container容器重定向到 URL,但嵌套锚点转到与容器不同的 URL
【发布时间】:2017-02-16 17:23:19
【问题描述】:

所以我在 Shopify 中有一个模板,如果单击该模板,我会尝试将用户重定向到信息页面。除了该容器中有两个嵌套链接之外,这非常简单。 “了解更多”会转到容器应该进入的页面,但“立即购买”按钮不会。它需要转到立即购买页面。

我使用 jQuery 来完成此操作,因为将整个内容嵌套在锚标记中不起作用。

我目前拥有的 jquery:

$(document).ready(function() {
        $(".cross-sell-img-container").click(
          function() { window.location = $(this).find(".spec").attr("href");
          return false;
        });
        $(".purchase").click(function() {
          window.location = $(this).find(".purchase").attr("href");
          return false;
        });  
        //$(".cross-sell-img-container").click(function() {
          //window.location = $(this).find("a").attr("href");
          //return false;
        //});
      });

如果我单击任意位置,则在路线中返回未定义。

模板如下所示:

<div class="cross-sell-img-container">

    <div class="detailed-cross-sell">

      <a href="{{ product.url }}">
        <p class="detailed-cross-sell-title">{{ "The " | append: product.title }}</p>
      </a>

      <div class="detailed-cross-sell-image-container">
        {{ product.metafields.custom_fields["spec_preview_image"] | replace: '<img', '<img class="detailed-cross-sell-image"' }}
      </div>

      <div class="button-container">
        {% assign shop_url = product.metafields.custom_fields["domehastore.com_link"] %}
        <a href="{% if shop_url %}{{ shop_url }}{% else %}{{ product.url }}#purchasing-area{% endif %}" class="button purchase" target="_blank">
          <span class="add-to-cart__text">Buy Now</span>
        </a>
        <!-- | replace: "/products", "/pages" | append: "-specifications" FP: add after product.url to make go to specs -->
        {% assign link_name = product.url %}
        <a class="button spec" href="{{ link_name }}">
          Learn More
        </a>
      </div>

    </div>

    <div class="preview-cross-sell">
      <a class="preview-image" href="{{ product.url }}">
        {{ product.metafields.custom_fields.cross_sell_preview_image }}
      </a>
    </div>

  </div>

我如何完成我想做的事情?

谢谢!

【问题讨论】:

    标签: javascript jquery html shopify liquid


    【解决方案1】:

    &lt;a&gt; 点击时阻止默认并停止传播

    $(".purchase").click(function(e) {
        e.preventDefault()
        e.stopPropagation();
        window.location = $(this).find(".purchase").attr("href");    
    });  
    

    或者对两者都使用一个处理程序并检查事件的目标

    $(".cross-sell-img-container").click(function(event) {
      var $link = $(event.target).closest('a.purchase');
      if ($link.length) {
        window.location = $link.attr('href');
      } else {
        window.location = $(this).find(".spec").attr("href");
      }
    
    });
    

    【讨论】:

    • 加一个..第二个解决方案完美!谢谢!!
    猜你喜欢
    • 2017-03-03
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 2018-04-05
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多