0

一連の画像の SRC を検索して、文字列に一致するものをすべて返そうとしています。私は非常に制限的な電子商取引システムを構築しています。画像をそのバリアントにリンクする方法がないため、すべての画像のリストを検索して、一致した src を返す必要があります。(顧客が WYSIWYG を使用して新しい色を追加できるように、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);
       });
    });

これは、1つの画像に対して完全に機能します。しかし、私は必要です

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);
4

3 に答える 3

2

前の質問への回答に、値attrAllの配列を返す関数を含めましたsrc。次に、それにインデックスを付けるだけです。

その関数は次のとおりです。

// A utility function from my arsenal; you can
// just inline this if you don't want it.
// Returns an array containing the given attribute
// from *all* of the elements in the jQuery object.
// Args:
//  name        Name of the attribute to fetch
//  blanksOkay  (Optional, default false) true if
//              you want blanks in the array for
//              blank entries or non-existant entries;
//              false if you want them left out.
$.fn.attrAll = function(name, blanksOkay) {
  var list, index;

  if (typeof blanksOkay === "undefined") {
    blanksOkay = false;
  }

  list = [];
  for (index = 0; index < this.length; ++index) {
    entry = $(this[index]).attr(name);
    if (entry || blanksOkay) {
      list.push(entry);
    }
  }
  return list;
};

使用法:

var srcArray = $('#preload img[src*=' + keyword + ']').attrAll('src');

「...最初に一致したSRC」を渡す:

$('div.image img').attr('src', srcArray[0]);

「...残りはすべて別のdivに」という意味がわかりませんが、それは配列です。通常の方法で値を取得し、それらを使用してimgdivなどにタグを追加できます。

おそらく、他のすべての画像imgdiv.thumbs. もしそうなら、その答えからの別の汎用関数が役立つかもしれません:

// Join the entries in the array with the given
// prefix and suffix.
function joinEntries(a, prefix, suffix) {
  prefix = prefix || '';
  suffix = suffix || '';
  return prefix + a.join(suffix + prefix) + suffix;
}

...だから、これを行うことができます:

$('div.thumbs').html(joinEntries(srcArray.slice(1), "<img src='", "'>"));

img...これにより、配列内の残りのすべてのエントリのサムがタグに置き換えられます。

于 2010-12-23T09:22:31.497 に答える
0
$('#preload img[src*=' + keyword + ']').each( function(index) {
    if (index == 0) { $('div.image img').attr('src', $(this).attr('src') ) }
    if (index == 1) { $('div.thumbs img').attr('src', $(this).attr('src') ) }
} ); 

最初の 2 つの後に何をしたいのか明確ではありません...

于 2010-12-23T09:25:21.857 に答える
0
$('#preload img[src*=' + keyword + ']').each(function(){
    var large_src = $(this).attr('src').replace('medium','large');
    // Set the image's source.
    $(this).attr('src', large_src);
});
于 2010-12-23T09:52:25.623 に答える