我在jQuery中有这样的代码:
children('table').children('tbody').children('tr').children('td')
它获取每行的所有表单元格。我的问题是:如何获取每行每个单元格中的文本值?
我应该使用.each()
来循环遍历所有的children('td')
吗?如何获取每个td
的文本值
发布于 2011-11-30 20:06:40
首先,你的选择器是过度杀伤力。我建议使用如下示例所示的类或ID选择器。一旦纠正了选择器,只需使用jQuery的.each()遍历集合:
ID选择器:
$('#mytable td').each(function() {
var cellText = $(this).html();
});
类选择器:
$('.myTableClass td').each(function() {
var cellText = $(this).html();
});
附加信息:
看看jQuery's selector docs吧。
发布于 2011-11-30 20:08:22
您可以使用.map
:http://jsfiddle.net/9ndcL/1/。
// array of text of each td
var texts = $("td").map(function() {
return $(this).text();
});
发布于 2011-11-30 20:06:30
我会给你的tds一个特定的类,例如data-cell,然后使用类似这样的东西:
$("td.data-cell").each(function () {
// 'this' is now the raw td DOM element
var txt = $(this).html();
});
https://stackoverflow.com/questions/8325655
复制相似问题