在 jQuery 中使用 nth-child 选择器,这应该可以工作:
$("#table").find("td:nth-child(2)").each(function () {
});
这使用nth-child 选择器http://api.jquery.com/nth-child-selector/,作为链接状态,它将选择所有<td> 元素,它们是其父元素的第二个子元素(即<tr>)。
这是一个演示它的小提琴:http://jsfiddle.net/GshRz/
如果您正在寻找一个选择器来获取仅在表中立即出现的<td>s(例如不在嵌套表中),请使用类似:
$("#table").children("tbody").children("tr").children("td:nth-child(2)").each(function () {
});
http://jsfiddle.net/GshRz/1/
根据您的结构(可能包含<thead>),您可以使用.children("thead, tbody") 而不仅仅是.children("tbody")。
另外,如果您想抓取几列,选择<tr> 元素然后获取它们的子元素<td> 可能会更容易。例如:
$("#table1").children("tbody").children("tr").each(function (i) {
var $this = $(this);
var my_td = $this.children("td");
var second_col = my_td.eq(1);
var third_col = my_td.eq(2);
console.log("Second Column Value (row " + i + "): " + second_col.html());
console.log("Third Column Value (row " + i + "): " + third_col.html());
});
http://jsfiddle.net/GshRz/2/
您使用什么选择器以及在哪里使用取决于您的表格的结构和内容。所以请记住区分children 和find,以及nth-child 和eq。