属性包含数组中的选择器

时间:2010-12-23 09:17:13

标签: jquery arrays attr

我正在尝试搜索一组图像的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);

3 个答案:

答案 0 :(得分:2)

my answer to your previous question中,我添加了一个函数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”,但它是一个数组,你可以用通常的方式从中获取值并使用它们来添加{{1} }标签到div或其他。

也许您的意思是您希望img中的所有其他图片都是img个标签。如果是这样,来自that answer另一个通用函数可能很有用:

div.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='", "'>")); 标记。

答案 1 :(得分: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 :(得分: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);
});
相关问题