2014-03-05 66 views
0

如何檢索每個元素?:獲取索引位置()

$(myArray).each(function() { 
console.log(...this.indexOf()?....) 
} 
+1

FYI'。每()'應該只用於對象相對jQuery來DOM節點,在這裏你應該使用:'$。每個(myArray的,函數(指數){的console.log (index);})' –

回答

2

您將獲得當前項目的索引作爲第一個參數的回調函數的索引位置$.each()

$(myArray).each(function(i) { 
    console.log(i); 
}) 

演示:Fiddle

3

閱讀docs。回調的第一個參數是索引。

所以:

$(myArray).each(function(index,item) { 
    console.log(index); 
}); 

旁註:Array.forEach需要它的順序相反的論點(項目第一,然後指數)的,恕我直言,感覺更自然。我不知道爲什麼jQuery最終放棄了這些論據。

+0

+1參考文檔。 – noobcode

0

jQuery.each

$(myArray).each(function(index, value) { 
console.log(index); 
}