jQuery获取表中单击的tr的第二到第十td的值。 我已经有了第一个

$("#existcustomers tr").click(function () { var td1 = $(this).children("td").first().text(); alert(td1); }); 

我也需要td2-td10的值。 我似乎无法弄清楚如何实现这一目标。 我尝试以同样的方式使用.second() ,但这似乎打破了编程。 有谁知道如何为以下的td实现这一目标?

要按索引获取特定单元格,您可以使用:

 $(this).children(":eq(1)") 

要获得前10个孩子,请使用:

 $(this).children(":lt(10)") 

如果要将内容放在数组的单独单元格中,则可以执行此操作

 var texts = $(this).children(":lt(10)").map(function(){return $(this).text()}); 

这样就形成了一个这样的数组:

 ["contentofcell1", "cell2", "3", "cell 4", "five", "six", "sieben", "otto", "neuf", "X"] 

使用eq(index)可以轻松找到它。

 $("#existcustomers tr").click(function () { var td1 = $(this).children("td").first().text(); var td2 = $(this).find("td").eq(2).text(); var td10 = $(this).find("td").eq(10).text(); alert(td1 + "-" + td2 + "-" + td10); }); 

获取td2 – td10范围的值:

 $("#existcustomers tr").click(function () { var td1 = $(this).children("td").first().text(); var result = ""; for(var i=2; i<=10; i++) { result = result + " - " + $(this).find("td").eq(i).text(); } alert(td1 + result); }); 
 $(this).children("td").each(function() { alert($(this).text()); } 

将循环遍历所有td

试试这个

 $("#existcustomers tr").click(function() { var td1 = ""; // To get values of td's between 2 and 10 we should search for // the td's greater than 1 and less than 11... $.each($(this).children("td:lt(11):gt(1)"),function() { td1 += $(this).text(); }); alert(td1); });