【问题标题】:Pulling alt/title tags from a thumbnail image when swapping out featured image?换出特色图片时从缩略图中提取 alt/title 标签?
【发布时间】:2019-11-20 00:22:34
【问题描述】:

我正在使用一个非常基本的 Javascript 函数来根据用户点击的缩略图来交换特色图像。图像可以很好地交换,但标题/alt 标签始终与页面加载时显示的默认特色图像相同。我想知道如何在选择缩略图时从缩略图中提取 alt/title 标签。非常感谢任何帮助!

UPDATE 已编辑以反映工作代码,感谢 ROK!

HTML:

<div id="product-photos">

{% if product.images.size == 0 %}

  <div id="product-photo-container">
    <img src="{{ '' | product_img_url: 'grande' }}" title="{{ image.alt | escape }}" title="{{ image.alt | escape }}" alt="{{ image.alt | escape }}" />
  </div>

{% else %}

  <div id="product-photo-container">
    <img src="{{ product.featured_image.src | product_img_url: 'grande' }}" title="{{ product.featured_image.alt | escape }}" alt="{{ product.featured_image.alt | escape }}" />
  </div>

  {% if product.images.size > 1 %}
  <ul id="product-photo-thumbs" class="clearfix grid">
    {% for image in product.images %}
    <li class="product-photo-thumb">
      <a href="{{ image.src | product_img_url: 'grande' }}" data-title="{{ image.alt }}" data-alt="{{ image.alt }}">
        <img src="{{ image.src | product_img_url: 'small' }}" title="{{ image.alt | escape }}" alt="{{ image.alt | escape }}" />
      </a>
    </li>
    {% endfor %}
  </ul>
  {% endif %}      

{% endif %}

</div><!-- #product-photos -->

JavaScript:

// Load variant image into feature area
$('.product-photo-thumb a').click(function(e) { e.preventDefault();
    $elem = $(this);
    var newAttributes = {
        src: $elem.attr('href'),
        title: $elem.data('title'),
        alt: $elem.data('alt')
    }
    $('#product-photo-container img').attr(newAttributes);
});

【问题讨论】:

    标签: javascript thumbnails swap


    【解决方案1】:

    用于此的 Javascript 看起来像这样:

    // Load variant image into feature area
    $('.product-photo-thumb a').click(function(e) { e.preventDefault();
        $elem = $(this).find('img')[0];
        var newAttributes = {
            src: $elem.attr('src'),
            title: $elem.attr('title'),
            alt: $elem.attr('alt')
        }
        $('#product-photo-container img').attr(newAttributes);
    });
    

    直接从图像中读取所有参数并将它们重新应用到特征 img。

    【讨论】:

    • 你就是那个男人!那成功了,我唯一改变的是我对数据标题和数据alt都使用了“image.alt”,这样它就可以在Shopify中工作了。非常感谢!
    • Np。也许我一开始就把它复杂化了。我编辑了帖子。这应该适用于您的原始 html。我没有注意到您在 img 上已经有了这些数据,最好不要复制 html。
    猜你喜欢
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 2017-03-21
    • 1970-01-01
    相关资源
    最近更新 更多