jquery - 获取 html 表指定列中的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5475142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:13:53  来源:igfitidea点击:

jquery - get elements in a specified columns of an html table

jqueryhtml

提问by vondip

Using jquery, I'd like to get all elements in a specified columns of an html table. Please note that it can be more than one column

使用 jquery,我想获取 html 表的指定列中的所有元素。请注意,它可以超过一列

For example, if I have the following html table:

例如,如果我有以下 html 表:

<table>
   <tr> 
    <td>
      a
    </td>
    <td>
      b
    </td>
    <td>
      c
    </td>
   </tr>
   <tr> 
    <td>
      1
    </td>
    <td>
      2
    </td>
    <td>
      3
    </td>
   </tr>
</table>

which looks as following:

如下所示:

1     2      3
a     b      c

I would like to get 1, 3, a , c

我想得到 1, 3, a , c

How should I do it? What would be the most efficient way to do so? (I am traversing a huge table generated by some reporting utility)

我该怎么做?这样做的最有效方法是什么?(我正在遍历由某些报告实用程序生成的巨大表格)

回答by Kaivosukeltaja

You can use the :nth-child()selector.

您可以使用:nth-child()选择器。

$("tr td:nth-child(1), tr td:nth-child(3)").css('color', 'red');

回答by Shadow Wizard is Ear For You

Here is more or less generic example letting you define the desired indices as array:

这是或多或少的通用示例,可​​让您将所需的索引定义为数组:

var cellIndexMapping = { 0: true, 2: true };
var data = [];

$("#MyTable tr").each(function(rowIndex) {
    $(this).find("td").each(function(cellIndex) {
        if (cellIndexMapping[cellIndex])
            data.push($(this).text());
    });
});

$("#Console").html(data.join("<br />"));

Test case: http://jsfiddle.net/yahavbr/FuDh2/

测试用例:http: //jsfiddle.net/yahavbr/FuDh2/

Using associative array to have faster performance, as far as I know search for specific item in such array should be optimized already.

使用关联数组以获得更快的性能,据我所知,在此类数组中搜索特定项目应该已经优化。

Note that in JS first index is always 0, so 1stand 3rdcells means indices 0 and 2.

注意,在JS第一索引始终为0,所以1和3个RD细胞手段索引0和2。

回答by John Fisher

var table = $("table"); // Make this more accurate if there are multiple tables on the page
var rows = table.find("tr");  // Will find too much if there are <tr> tags nested in each other

for (var i=0; i=rows.length; ++i) {
  var cells = rows.eq(i).find("td");
  var values = [cells.eq(0).text(), cells.eq(2).text()];
}

回答by Arun

You can do it with a selector using nth-child. The following is another way.

您可以通过使用 nth-child 的选择器来完成。下面是另一种方式。

$("TABLE").find("tr").each(function(){
    var tr = this;
    $([0,2]).each(function(){
        alert($(tr[this]).html());
    });
});

For 1st and 3rd you'll have to specify 0, 2

对于 1st 和 3rd,您必须指定 0, 2

回答by sergio

you can do like this

你可以这样做

var retorno=[];
linha=$("td:nth-child(1), td:nth-child(3)").clone().each(function(){retorno.push($(this).text())});

$.each(retorno,function(index, item) {$('#Console').append(item+', ');});

1-use clone to not modify table, if you remove table modify after this line. 2-put elements into retorno array 3-use each for print inside element Console

1-使用克隆不修改表,如果在此行之后删除表修改。2-将元素放入 retorno 数组 3-使用每个元素在元素控制台内打印

you can make too like this. with one line:

你也可以这样。一行:

$("td:nth-child(1), td:nth-child(3)").clone().map(function(){ return $(this).text()}).get().join(',');

map - create array with return columns get - get the array join - chang array in line separete with comma and show

映射 - 创建带有返回列的数组 get - 获取数组连接 - 用逗号分隔行中的更改数组并显示

1,3,a,c

1,3,a,c

it's the best i can do.

这是我能做的最好的。