2011-05-27 42 views
0

編輯,這種解決問題的每個jQuery中的每一個?

要我好象我們需要循環3次這樣的代碼使用數組,感謝亞歷山大

var str = ''; 
    $.each(cores, function(i, value) { 
     $.each(value, function(j, value_2) { 
      $.each(value_2, function(k, sel_cor) { 
       console.log(sel_cor); 
       str += "<div class='cores' title="+sel_cor+" style='background-color:#"+sel_cor+"'></div>"; 
      }); 
     }); 
    }); 

我試圖做一個jQuery的顏色拾取。

我有3個十六進制顏色的數組,我想爲每一個構建小方格,所以我需要每個數組的每個數組。

這可能嗎?如何才能做到這一點?

cores[0] = [["e3e3e7","e5e5e6","ecdfec","e6dfec","dfdfe6","dfe6e6","dfe6df","e8e8e0","f9f2df","f4e9df","ecdfdf","e5e4e2"]]; 
cores[1] = [["e3e3e7","e5e5e6","ecdfec","e6dfec","dfdfe6","dfe6e6","dfe6df","e8e8e0","f9f2df","f4e9df","ecdfdf","e5e4e2"]]; 
cores[2] = [["e3e3e7","e5e5e6","ecdfec","e6dfec","dfdfe6","dfe6e6","dfe6df","e8e8e0","f9f2df","f4e9df","ecdfdf","e5e4e2"]]; 

$.each(cores, function(j, value) { 
      console.log('j = '+j+' value = '+value); 
      str_claras += "<div class='tile' style='width:20px; height:20px; padding:0;margin:0;border:1px solid #333333;cursor:pointer;background-color:#"+j[value]+"'></div>"; 
    }); 

回答

2

難道你不能只使用內置的JavaScript的循環結構?

for(core in cores) 
{ 
    for(blah in blahs) 
    { 
     //... 
    } 
} 
+1

是的,我想知道是否有一個jQuery解決方案還:-) – Pluda 2011-05-27 16:03:04

2
$.each(cores, function(j, value) { 

      $.each(value, function(i, value2) { 

       str_claras += "<div class='tile' style='width:20px; height:20px; padding:0;margin:0;border:1px solid #333333;cursor:pointer;background-color:#"+value2+"'></div>"; 
     }); 
}); 

這裏是鏈接:Click Here

+0

感謝亞歷山大還沒有,我們仍然有來自陣列ababb7所有值, b0b1b4,c69fc6等等:-( – Pluda 2011-05-27 16:14:46

+0

試試這個鏈接: http://jsfiddle.net/corotchi/wWcYr/ – AlexC 2011-05-27 17:57:21

+0

但我的陣列不像你放在那裏的那個,它的核心[0] = [[「 e3e3e7「,」e5e5e6「,」ecdfec「,」e6dfec「,」dfdfe6「,」dfe6e6「,」dfe6df「,」e8e8e0「,」f9f2df「,」f4e9df「,」ecdfdf「,」e5e4e2「]];那是另一個數組中的數組使密度可以分開顏色:-)謝謝! – Pluda 2011-05-27 18:14:14