2013-03-01 138 views
-1

我試圖從特定行中獲取所有td's使用jQuery遍歷表中的元素

下面是代碼

<script> 
$(window).scroll(function() { 
var widthArray = new Array(); 
var i=0; 
$('#firstRow >td').each(function(){ 
var tdclass=$('td').attr('class'); 
console.log(tdclass); 
widthArray.push(tdclass); 
    i=i+1; 
}); 
}); 

但它停靠在第一td而不是迭代。

更新:i變量用於不同的功能。

回答

1

您必須在.each()循環內使用$(this)

$('#firstRow > td').each(function(){ 
    var tdclass = $(this).attr('class'); 
    console.log(tdclass); 
    widthArray.push(tdclass); 
    i=i+1; 
}); 
+0

謝謝......保存了一天,我會接受答案,當SO讓我在10分鐘左右這樣做。 – lazyprogrammer 2013-03-01 08:35:30

1

每次拿到班級第一<td>元素的集合中的$('td')迭代。你可以改變你的建設更優雅:

$(window).scroll(function() { 
    var widthArray = $("#firstRow > td").map(function() { 
      return this.className; 
     }).get(), 
     i = widthArray.length; 

    // ... 
}); 
+0

謝謝,但@布魯諾的答案做到了......無論如何感謝 – lazyprogrammer 2013-03-01 08:35:56