我試圖搜索一組圖像的SRC,然後返回所有匹配的字符串。我構建的電子商務系統非常有限,沒有辦法將圖像鏈接到它的變體,這就是爲什麼我必須搜索所有圖像的列表,然後返回匹配的src。 (我還使用的IF語句是這樣的話,客戶可以使用所見即所得,並添加新的色彩機架)屬性包含選擇器到數組
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);