我正在尝试搜索一组图像的 SRC,然后返回所有匹配的字符串。我正在建立一个非常严格的电子商务系统,没有办法将图像链接到它的变体,所以这就是为什么我必须搜索所有图像的列表然后返回匹配的 src。(我也在使用一组 IF 语句,这样客户就可以使用 WYSIWYG 并添加新颜色)
jQuery(document).ready(function($) {
$('#product-variants-option-0').change(function() {
var select_value, keyword, new_src;
select_value = $(this).find(':selected').val();
if (select_value == "Kelly Green") { keyword = "kelly"; };
if (select_value == "Navy") { keyword = "navy"; };
if (select_value == "Gunmetal Heather") { keyword = "gunmetal"; };
if (select_value == "Olive") { keyword = "olive"; };
if (select_value == "Cocoa") { keyword = "cocoa"; };
if (select_value == "Black") { keyword = "black"; };
if (select_value == "Charcoal") { keyword = "charcoal"; };
if (select_value == "Dark Teal") { keyword = "teal"; };
if (select_value == "White") { keyword = "white"; };
if (select_value == "Black") { keyword = "black"; };
if (select_value == "Garnet") { keyword = "garnet"; };
if (select_value == "Charcoal Heather") { keyword = "charcoal-heather"; };
// Find the image using that `src`, note that we concatenate the value
// from `keyword`, rather than having it in a literal.
var new_src = $('#preload img[src*=' + keyword + ']').attr('src');
var large_src = new_src.replace('medium','large');
// Set the image's source.
$('div.image img').attr('src', large_src);
});
});
对于一张图像,这非常有效。但是我需要
var new_src = $('#preload img[src*=' + keyword + ']').attr('src');
作为一个数组。然后将第一个匹配的 SRC 传递给
$('div.image img').attr('src', large_src);
然后所有其余的到另一个 div
$('div.thumbs img').attr('src', new_src);