Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery从td单元格获取文本_Javascript_Jquery - Fatal编程技术网

Javascript 使用jQuery从td单元格获取文本

Javascript 使用jQuery从td单元格获取文本,javascript,jquery,Javascript,Jquery,我在jQuery中有以下代码: children('table').children('tbody').children('tr').children('td') 它获取每行的所有表格单元格。我的问题是:如何获得每行中每个单元格中的文本值 我应该使用.each()在所有子项('td')中循环吗?如何获取每个td的文本值?我会为您的tds指定一个特定的类,例如数据单元,然后使用如下内容: $("td.data-cell").each(function () { // 'this' is

我在jQuery中有以下代码:

children('table').children('tbody').children('tr').children('td')
它获取每行的所有表格单元格。我的问题是:如何获得每行中每个单元格中的文本值


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

我会为您的tds指定一个特定的类,例如数据单元,然后使用如下内容:

$("td.data-cell").each(function () {
    // 'this' is now the raw td DOM element
    var txt = $(this).html();
});

首先,你的选择太过分了。我建议使用类或ID选择器,如下面的示例所示。更正选择器后,只需使用jQuery对集合进行迭代:

ID选择器:

$('#mytable td').each(function() {
    var cellText = $(this).html();    
});
$('.myTableClass td').each(function() {
    var cellText = $(this).html();    
});
类选择器:

$('#mytable td').each(function() {
    var cellText = $(this).html();    
});
$('.myTableClass td').each(function() {
    var cellText = $(this).html();    
});
其他信息:

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

看看。

你可以使用
.map


您希望如何处理此文本值?你要怎么格式化?请更详细地解释你的情况。我将把它作为纯文本使用。另外,一些字段需要作为数字/日期/时间,例如“我需要时间开始”。虽然您的代码可能会提供问题的答案,但请在其周围添加上下文,以便其他人了解它的作用和原因。