如何遍历表并读取每个行中的第一个列

如何使用jquery读取第一列的每个值?

我的代码不起作用。

$("foo:eq(1) tr").each(function(){ alert( $(this).find('td:first[rowspan]').text()); }); 
Monday5 Jim
Remy

修正了这里: http : //jsfiddle.net/tes9E/4/

首先,jQuery :eq选择器从零开始。 其次,你需要在选择id之前加一个# ,如下所示:

  $("#foo:eq(0) tr").each(function(){ alert( $(this).find('td:first[rowspan]').text() ); }); 

从jQuery文档:

.eq (索引)

index – 一个整数,指示元素的从0开始的位置

否则,一切看起来都很棒。 希望这可以帮助!

 $("#foo:eq(0) tr").each(function(){ //--> Noo # and eq starts from 0 alert( $(this).find('td:first[rowspan]').text()); }); 
Monday5 Jim
Remy

试试这个

编辑

JS应该是这样的

 $("#foo:eq(0) td").each(function(){ if($(this).attr('rowspan')!== undefined) { alert($(this).text()) } }); 

DEMO

 // A simple function that returns an array of cells within a specific column $.fn.column = function(i) { return $("tr td:nth-child(" + i + ")", this); }; // Using that function we can now select all the cells in the first column var $col1 = $table.column(1); // And then iterate over each one and do something $col1.each(function() { return alert($(this).text()); });