简体   繁体   English

获取表中的特定列

[英]Get a specific column in a table

What I want is to get a specific table column using jquery, so far what I have is this, that selects the first column: 我想要的是使用jquery获取特定的表列,到目前为止,我所拥有的是选择第一列的表:

table.find(tr > td:first-child)

But I want to be able to select any column so I can copy it to another table, is there a way to do this for example : 但是我希望能够选择任何列,以便可以将其复制到另一个表,例如,有没有一种方法可以做到这一点:

td:n-child 

so I can send it the number of column and get all the data from that specific column. 因此我可以向其发送列数,并从该特定列获取所有数据。

table.find("tr > td").eq(n);

我只是从心底写这个,所以我无法确认它是否有效,但是我认为这是执行此操作的语法。

:eq() Selector : Description: Select the element at index n within the matched set. :eq()选择器:说明:选择匹配集中索引n处的元素。

You could use :eq() Selector , eg : 您可以使用:eq() Selector ,例如:

$('tr > td:eq(n)')

Hope this helps. 希望这可以帮助。

 $('td:eq(2)').css('background-color','green') $('tr:eq(2) td:eq(0)').css('background-color','red') 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table border=1> <tr> <td>1</td> <td>A1</td> <td>B1</td> </tr> <tr> <td>2</td> <td>A2</td> <td>B2</td> </tr> <tr> <td>3</td> <td>A3</td> <td>B3</td> </tr> </table> 

Try this: 尝试这个:

for instance for selecting the 2nd element, you would: 例如,选择第二个元素,您将:

table.find("tr > td:nth-child(2)");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM