使用jQuery从td单元格获取文本 [英] Getting text from td cells with jQuery

查看:183
本文介绍了使用jQuery从td单元格获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jQuery中有这个代码:

I have this code in jQuery:

children('table').children('tbody').children('tr').children('td')

获取每行的所有表格单元格。我的问题是:如何在每行的每个单元格中获取文本值?

Which gets all table cells for each row. My question is: how can I get the text value in each cell in each row?

我应该使用 .each()循环所有子项('td')?如何获取每个 td 的文本值?

Should I use .each() to loop trough all children('td')? How can I get the text value of each td?

推荐答案

首先总之,你的选择器是矫枉过正的。我建议使用类或ID选择器,如下面的示例。一旦你纠正了你的选择器,只需使用jQuery的 .each()来迭代你的选择:

First of all, your selector is overkill. I suggest using a class or ID selector like my example below. Once you've corrected your selector, simply use jQuery's .each() to iterate through the collection:

ID选择器:

$('#mytable td').each(function() {
    var cellText = $(this).html();    
});

班级选择器:

$('.myTableClass td').each(function() {
    var cellText = $(this).html();    
});

附加信息:

查看 jQuery的选择器文档

这篇关于使用jQuery从td单元格获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆