我使用一个非常基本的Javscript函数来根据用户点击的缩略图交换特色图像。图像交换得很好,但title / alt标签始终与页面加载时显示的默认特色图像保持一致。我想知道如何在选择它们时从缩略图中拉出alt / title标签。任何帮助非常感谢!
更新 修改以反映韩国的工作代码!
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 -->
Javscript:
// 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);
});
答案 0 :(得分:1)
Javascript for this看起来像这样:
// 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。